Can anyone help me on how to update many to many relationship? I have tasks, user can have many tasks and task can have many users (many to many), What I want to achieve is that in update form admin can assign multiple users to specific task. This is done through html multiple select input
this is my controller
public function index()
{
// this is in main view
$tasks=Task::all()->load('users');
$userLogin_id=Auth::user()->id;
$users=User::all()->where('id','!=',$userLogin_id);
return view('adminUser.index',compact('tasks','users'));
}
public function store(Request $request)
{
// to create new task and for who it
$task =Task::create($request->all());
$userId=$request['employee'];
$task->users()->attach($userId);
return back();
}
public function edit($task_id)
{
//to go to view that has update code
$task=Task::find($task_id)->load('users');
$userLogin_id=Auth::user()->id;
$users=User::all()->where('id','!=',$userLogin_id);
return view('adminUser.edit',compact('task','users'));
}
public function update(Request $request, $task_id)
{
//to update task
$user_id=$request['employee'];
Task::where('id',$task_id)
->update(['title'=>$request['title'],
'description'=>$request['description'],
'status'=>$request['status'],
'file_path'=>$request['file_path']]);
//what i can do complete this method if it is correct
return redirect('/admin');
}
this my user model
public function tasks(){
return $this->belongsToMany('App\Task','Todos','user_id','task_id');
}
this my task model
public function users(){
return $this->belongsToMany('App\User','todos','task_id','user_id');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire