Laravel sync()方法错误

Short explanation of what I have: a ManyOnMany relation between project and user using a pivot table called project_user.

When showing a certain project a list of users is printed with checkboxes so they will be linked to the project as soon as the checkbox is checked (so it will send the user_id + project_id to the pivot table

enter image description here

Now the problem. When we have the user (for example Admin der Liam) linked to the project it will work and the data will be send to the database. But when the checkbox is unchecked again it will give a Call to a member function users() on a non-object error instead of clearing the table and putting only the checked boxes into the database

Controller:

public function edit($id, Project $project)
{
    $users = User::with('projects')->get();

    $project = $this->project->find($id);

    return view('project.edit', ['project' => $project, 'id' => 'edit', 'project_id' => $id, 'users' => $users]);  
}  


public function update(CreateProjectRequest $request)
{
    if($request->get('contribute'))
    {
        foreach($request->get('contribute') as $k => $contribute)
        {
            if($contribute == 1)
            {
                $project = $this->project->find($request->project_id);
                $keys[] = $k;
            }
        }
    }
    $project->users()->sync($keys);

    $project = $this->project->find($request->project_id);
    $project->fill($request->input())->save();

    return redirect('project');
}

HTML/Blade:

@if(isset($users))
@foreach($users as $user)
    <tr>
        <td>
             {{$user->firstname}} {{$user->middlename}} {{$user->lastname}}
        </td>
        <td>
             {!! Form::checkbox('contribute['.$user->id.']', '1', $user->projects->contains($project->id)) !!}
        </td>
   </tr>
@endforeach
@endif
public function update(CreateProjectRequest $request)
{
    $this->project = $this->project->find($request->project_id);
    if($request->get('contribute'))
    {
        foreach($request->get('contribute') as $k => $contribute)
        {
            if($contribute == 1)
            {
                $keys[] = $k;
            }
        }
    }

    if(isset($keys)) {
        $this->project->users()->sync($keys);
    }
    else {
        $this->project->users()->detach();
    }

    $this->project = $this->project->find($request->project_id);
    $this->project->fill($request->input())->save();

    return redirect('project');
}

Your $project is empty because you declared $project in the foreach. Try to declare it outside the foreach.

Put the $project->users()->sync($keys); after the $project = $this->project->find($request->project_id); in your while loop. You should sync your $project after you have initialized the object.

foreach($request->get('contribute') as $k => $contribute)
{
    if($contribute == 1)
    {
        $project = $this->project->find($request->project_id);
        $keys[] = $k;
        $project->users()->sync($keys);
    }
}