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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口