arash kenji
arash kenji

Reputation: 31

Dynamic update json table field

I have simple to-do app each task have multi sub task (json formatted) like this

[{"task": "task1", "complete": 0}, {"task": "task2", "complete": 0}]

i want to user clicked on each sub task "complete" toggle between 0 or 1 the routing is working but i cant update for example 'task1' 'complete' to 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');

Upvotes: 0

Views: 81

Answers (1)

Mohamed Ahmed
Mohamed Ahmed

Reputation: 223

I assume you do not have separate table for sub tasks. In your case, this is what I would do. Create a sub table with relation to the main table.

For the sake of this example, lets call our tables Task and SubTask. So the model names we generate would be as Task and SubTask. Now for the relation.

Inside your Task model, you will have this:

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

And inside your SubTask model, you will have this:

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

That's it for the relationship As for your database tables, it would look something like this:

tasks table:

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

subtasks table:

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

Now in your controller, to display the tasks with subtasks you could do this:

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'));
}

And to update a 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!';

}

As for your SubTasks, same follows (kinda):

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!';

}

As for your routes, it will look something like this:

//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');

Once all this is done, you may want to use a button as a "Press to Complete" or something. And in your Index blade file (showtask.index), you would probably do something like this:

@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! That should be more than enough for a complete ToDOList. This shall get you started. The relationships are shown. The Controller Methods are shown. The Routes are shown. And so are the display and buttons to update.

Note: This code was not tested and this was on top of my head. Also I haven't used laravel in a while so I am a bit rusty

Hope this was enough. Happy Coding :)

Upvotes: 1

Related Questions