简体   繁体   English

Laravel Sync方法仅发送第二个数据

[英]Laravel Sync method only sending the 2nd data

This piece of code should be deleting all old data from the database and when add the new ones (using sync()) 这段代码应该是从数据库中删除所有旧数据以及添加新数据时(使用sync())

Now I have a project with users, and a user can be linked to a project with a checkbox. 现在我有一个用户项目,用户可以通过复选框链接到项目。

So on checkbox checked this function will trigger, but for example when I say that user 1 and user 2 are going through this fuction to get added to the pivot table it will only send user 2 , and user 1 will not get through, what is going wrong? 因此,在复选框上选中此功能将触发,但是例如当我说user 1user 2正在通过此功能添加到pivot table时它将只发送user 2 ,而user 1将无法通过,这是什么出错了?

And when I add 3 users user 1 , user 2 , user 3 , only user 2 will get added. 当我添加3个user 1user 2user 3 ,只会添加user 2

Controller 调节器

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);
                $project->users()->sync(array($k));

            }
        }
    }

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

    return redirect('project');
}

Blade

@foreach($users as $user)
            <tr>
                <td>
                    {{$user->firstname}} {{$user->middlename}} {{$user->lastname}}
                </td>
                <td>
                    {!! Form::checkbox('contribute['.$user->id.']', '1', $user->projects->contains('id', $project->id)) !!}
                </td>
            </tr>
@endforeach

On a dd($request->input()); dd($request->input()); at the start of my update method (with selecting atleast 3 users) this will get in return: 在我的更新方法开始时(选择至少3个用户),这将得到回报:

  array:9 [▼
  "_method" => "PATCH"
  "_token" => "0uIZNn6zwZjVKfgE0ckhDULeYda0OaLzKVdUgoM8"
  "name" => "Dire Straits"
  "completion_date" => "2015-05-18"
  "DataTables_Table_0_length" => "10"
  "contribute" => array:3 [▼
    1 => "1"
    3 => "1"
    2 => "1"
  ]
  "completed" => "1"
  "active" => "0"
  "project_id" => "11"
]

So 1 / 3 / 2 would be the user_id and => 1 should be the value. 所以1 / 3 / 2 user_iduser_id=> 1应该是值。

The problem is that sync gets called in loop 3 times so each time it's syncing one value. 问题是syncloop被调用3次,所以每次同步一个值。 You have to pass an array of the ids in sync ex: 你必须在sync ex中传递一组id:

$project->users()->sync([1,3,2]);

Or if you want you can use attach when contribute==1 and detach when contribute==0 或者如果你想要,你可以在contribute==1时使用attach ,并在contribute==0detach

Or if contribute doesnt return input when a user is deselected and it only returns when it is selected then you can try: 或者,如果在取消选择用户时, contribute不返回输入,并且仅在选择用户时返回,则可以尝试:

$this->project->users()->sync(array_keys($request->get('contribute'));

I just noticed that you have another bug unless you are updating many projects with one call you should put the line below on the first line of your function. 我只是注意到你有另一个bug,除非你通过一次调用更新许多项目,你应该将下面的行放在你函数的第一行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM