动态更新json表字段

时间:2019-08-14 07:04:56

标签: json laravel

我有简单的待办应用 每个任务都有多个子任务(json格式) 像这样

[{“ task”:“ task1”,“ complete”:0},{“ task”:“ task2”,“ complete”:0}]

我要用户单击每个子任务“完成”,在0或1之间切换 路由正常,但我无法将“ task1”“ complete”更新为1

public function complete($name,$task_id){
    //return($name .'  '.$task_id);
    $NameTodoList = TodoList::where('name', $name)->first();
    //update this 
      dd($NameTodoList->task[$task_id]["complete"]);
    //update this 
}



Route::get('/todo/{name}/edit/{task_id}','TodoListController@complete')->name('task.edit');

1 个答案:

答案 0 :(得分:1)

我假设您没有用于子任务的单独表格。在您的情况下,这就是我要做的。创建一个与主表相关的子表。

为了这个例子,让我们调用表TaskSubTask。因此,我们生成的模型名称将为TaskSubTask。现在是关系。

在您的Task模型中,您将拥有:

public function subtasks(){
  return $this->hasMany(SubTask::class);
}

在您的SubTask模型中,您将拥有:

public function task(){
  return $this->belongsTo(Task::class);
}

仅此而已 对于您的数据库表,它看起来像这样:

tasks表:

id -> int -> autoincrement
task_name -> varchar
task_details -> varchar
isCompleted -> boolean
created_at
updated_at

subtasks表:

id -> int -> autoincrement
task_id -> int -> autoincrement
task_name -> varchar
task_details -> varchar
isCompleted -> boolean
created_at
updated_at

现在在您的控制器中,要显示带有子任务的任务,您可以执行以下操作:

public function index(){
  $task = Task::with('subtasks')->get(); //Here is the relationship we created earlier. the 'with()' method takes in relationship
  //More over, you could add more where clauses like where('isCompleted', 1)
  return view('showtask.index', compact('task'));
}

并更新任务:

public function updateTask($id){

  $update = Task::findOrFail($id);
  //We are finding the task by id. if it does not exist, it fails. Now you have access to the task. Lets do an update.

  $update->isCompleted = 1; //isComplete is set to 1
  $update->update(); //Finally do the update

 return 'Done!';

}

关于您的子任务,如下(类似):

public function updateSubTask($id){

  $update = Task::findOrFail($id);
  //We are finding the Subtask by id. if it does not exist, it fails. Now you have access to the Subtask. Lets do an update.

  $update->isCompleted = 1; //isComplete is set to 1
  $update->update(); //Finally do the update

 return 'Done!';

}

关于您的路线,它看起来像这样:

//To Display Tasks
Route::get('/displayTask','TodoListController@index')->name('task.index');

//To Update Task
Route::get('/updateTask/{id}','TodoListController@updateTask')->name('task.update');

//To Update SubTask
Route::get('/updateSubTask/{id}','TodoListController@updateSubTask')->name('subtask.update');

完成所有这些操作后,您可能希望将按钮用作“按完成”按钮。在索引刀片文件(showtask.index)中,您可能会执行以下操作:

@foreach($task as $tasks)

{{ $tasks->task_name }}

<a href={{ route('task.update', $tasks->id) }} class="btn btn-success">Task Completed</a> <!-- Button Assumes you are using bootstrap -->

<!-- Now to access the subtask,  you will open another foreach loop inside the current 
foreach loop as they are related -->


    @foreach($tasks as $subtasks)

       {{ subtasks->name }}

        <a href={{ route('subtask.update', $subtasks->id) }} class="btn btn-success">Task Completed</a> <!-- Button Assumes you are using bootstrap -->

    @endforeach


@endforeach

PHEW!对于完整的ToDOList,这应该绰绰有余。这将使您入门。关系被显示。显示了控制器方法。显示路线。显示和要更新的按钮也是如此。

注意:该代码未经测试,这是我的首要任务。而且我有一段时间没有使用laravel了,所以我有点生锈

希望这足够了。快乐编码:)