1

I created 3 models and in my case I need to link the link of 3 models in the table:

user:
 name
 password
 ...

task:
 ...
 task_status_id

task_status:
 name
 ...

task_user:
 task_id
 user_id

but in my implementation me need task status for all users,like:

task_user:
 task_id
 user_id
 task_status_id

because one of list users can finish task but not another

if anyone can tell me best way solution for this,thanks!

1 Answers1

0

You can add additional columns in your pivot table. So you would just add the additional column in your migration file.

Then in your model relationship you need to add an additional function call.

return $this->belongsToMany('App\Task')->withPivot('task_status_id');

Then you can set like so.

$user->tasks()->attach($task->id, ['task_status_id' => $task_status->id]);

There is more info in the Laravel Documentation

Many to Many Pivot Update

ColinMD
  • 952
  • 6
  • 14
  • Will I be able to use this field for queries to the database using eloquent? task :: users () -> with ('task_status') or something like that? – Алексей Цапаев May 03 '19 at 10:32
  • There is a 'wherePivot' function but I've never had to use it. Looking at the docs you would call tasks::users()->wherePivot('task_status_id', $task_status->id); – ColinMD May 03 '19 at 19:17