使用同步在Laravel的数据透视表中进行分离 [英] Using sync to detach in pivot table in Laravel

查看:125
本文介绍了使用同步在Laravel的数据透视表中进行分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个表(评估者,进程),我有一些奇怪的事情,我不明白和想知道是否有人可以帮助我解决发生了什么。 )和一个枢轴(process_assessor)。我正在尝试使用评估者的流程来更新枢轴,所以我有一个表单,当提交被传递到这个方法...

  public function updateProcesses($ id)
{
$ assessor = $ this-> model-> find($ id);
return $ evaluateor-> processes() - > sync($ this-> request-> get('process_id'));
}

这样工作正常,但是当更新用户没有进程时我收到错误消息...

 参数1传递给Illuminate\Database\Eloquent\Relations\BelongsToMany :: formatSyncList()必须是数组类型,null给定,称为

但是(这是奇怪的一点)我还有一个用户配置文件模块,它使用完全相同的方式与数据透视表进行同步,但是当我提交该表单时,该特定模块的所有选项都将被取消选中,然后它可以工作,用户在枢轴中脱离它们。 / p>

我做错了什么?任何想法?

解决方案

简单地转换为数组:

  return $ assessor-> processes() - > sync((array)$ this-> request-> get('process_id')); 






或者,您可以使用空数组作为默认值对于 get()

  return $ assessor-> ) - > sync($ this-> request-> get('process_id',[])); 

但是第一个选项更可靠。


I have something strange happening which I can't understand and wondered if anyone could help me work out what's going on.

I have 2 tables (assessors, processes) and a pivot (process_assessor). I am trying to update the pivot with an assessor's processes so I have a form which when submitted is passed to this method...

public function updateProcesses( $id )
{
    $assessor = $this->model->find( $id );
    return $assessor->processes()->sync( $this->request->get( 'process_id' ) );
}

This works fine however when updating a user to have no processes I get an error message...

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList() must be of the type array, null given, called

However, (and this is the weird bit) I also have a user profile module which uses exactly the same way to 'sync' with the pivot table however when I submit that form with all options for that particular module unchecked then it works, the user is detached from them in the pivot.

What am I doing wrong? Any ideas?

解决方案

Simply cast to an array:

return $assessor->processes()->sync( (array) $this->request->get('process_id') );


Alternatively you can use empty array as a default value for get():

return $assessor->processes()->sync( $this->request->get('process_id', []) );

however 1st option is more reliable.

这篇关于使用同步在Laravel的数据透视表中进行分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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