I have project and user table, i want to update attached user in project so i want to attach more or remove user to project
My code look like this
public function update(Request $request, $id)
{
$project = Project::find($id);
$project->update($request->all());
if ($project->users()->where('users.id',$request->input('user_id'))->count() == 0){
$project->users()->attach($request->input('user_id'));
}
else if($project->users()->where('users.id',$request->input('user_id'))->count() != 0){
$project->users()->detach($request->input('user_id'));
}
return response()->json($project);
}
This code not work correctly when add else if for detach user that want, so anyone help me to resolve this issue and thanks
I could finally figure out what you need based on the discussion in the chat.
public function update(Request $request, $id)
{
$project = Project::find($id);
$project->update($request->except('user_id'));
$project->users()->sync($request->input('user_id'));
return response()->json($project);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments