mardi 8 octobre 2019

Update a column before incrementing and decrementing a value on another table

I'm quite new to this so please forgive me for my level of coding. I have a small scale workload assignment application. It lets me transfer tasks between workers. The idea is a single worker can only have 3 tasks maximum. So i want to be able to transfer one task from one worker to another. Incrementing and decrementing their count in the process as well.

Workers table

id | name | totalTasks

Tasks table

id | title | description | worker_id

Controller

public function transferTask(Request $request)
{
 $taskid = $request['taskid'];
 $currentWorker = $request['currentWorker'];
 $newWorker = $request['newWorker'];

 DB::table('tasks')
 ->where('id', $taskid)
 ->update(['user_id' => $newWorker]);

 DB::table('workers')
 ->where('id', $currentWorker)
 ->decrement('totalTasks');

 DB::table('workers')
 ->where('id', $newWorker)
 ->increment('totalTasks');

 return redirect()->back()->with('message', 'Transfer Done');
}

I am able to transfer/update the task from one worker to another but i am unable to update the totalTasks of the workers that have their tasks removed from them and added to another.

What do i need to do to update the count on the totalTasks of my currentWorker and totalWorker. Any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire