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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?