doucha5080
2014-11-28 10:56
浏览 44
已采纳

使用同步在Laravel中的数据透视表中分离

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?

图片转代码服务由CSDN问答提供 功能建议

我发生了一些奇怪的事情,我无法理解,并想知道是否有人可以帮我弄清楚发生了什么。

我有2个表(评估者,进程)和一个数据透视表(process_assessor)。 我正在尝试使用评估者的流程更新数据透视,因此我有一个表单,当提交时传递给此方法...

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

这很好但是当更新用户没有进程时我收到错误消息...

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

然而,(这是奇怪的一点)我还有一个用户配置文件模块,它使用与数据透视表“同步”完全相同的方式但是当我提交该表单时,该特定模块的所有选项都未选中 它起作用,用户在枢轴中与它们分离。

我做错了什么? 有什么想法吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhuanqian8244 2014-11-28 11:29
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题