Laravel Sync方法仅发送第二个数据 [英] Laravel Sync method only sending the 2nd data

查看:56
本文介绍了Laravel Sync方法仅发送第二个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码应该从数据库中删除所有旧数据,并在添加新数据时(使用sync())

This piece of code should be deleting all old data from the database and when add the new ones (using sync())

现在我有一个包含用户的项目,并且可以通过复选框将用户链接到项目.

Now I have a project with users, and a user can be linked to a project with a checkbox.

因此,选中此复选框将触发此功能,但是例如,当我说 user 1 user 2 正在通过此功能添加到数据透视表,它将仅发送用户2 ,而用户1 无法通过,这是怎么回事?

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?

当我添加3个用户用户1 用户2 用户3 时,只有 user 2 被添加.

And when I add 3 users user 1, user 2, user 3, only user 2 will get added.

控制器

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');
}

刀片

@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

dd($ request-> input()); 上,在我的更新方法开始时(选择至少3个用户),这将得到回报:

On a dd($request->input()); at the start of my update method (with selecting atleast 3 users) this will get in return:

  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"
]

因此, 1/3/2 将是 user_id =>1 应该是值.

So 1 / 3 / 2 would be the user_id and => 1 should be the value.

推荐答案

问题是 sync loop 中被调用3次,因此每次同步一个值时.您必须在 sync ex:

The problem is that sync gets called in loop 3 times so each time it's syncing one value. You have to pass an array of the ids in sync ex:

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

或者,如果您愿意,可以在 contribute == 1 时使用 attach ,在 contribute == 0 时使用 detach >

Or if you want you can use attach when contribute==1 and detach when contribute==0

或者如果 contribute 在取消选择用户时不返回输入,而仅在选择用户时返回,则可以尝试:

Or if contribute doesnt return input when a user is deselected and it only returns when it is selected then you can try:

$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.

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

这篇关于Laravel Sync方法仅发送第二个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆