无法同步条件数据透视表多对多关系laravel

时间:2017-03-01 07:49:19

标签: php mysql laravel laravel-5.3

我有2个型号:Tours.php

 public function includes()
{
    return $this->belongsToMany('App\Included');
}

Included.php

public function tours()
{
    return $this->belongsToMany('App\Tour');
}

以及我的TourController.php store方法中的代码:

if (isset($request->includeds) && isset($request->excludeds)) {
      $tour->includes()->sync($request->includeds, false);
      $tour->excludes()->sync($request->excludeds, false);
}

当我dd($request->includeds);时,我可以看到表单中即将发布的值但无法在我的数据透视表included_tour中同步,而代码同步excluded_tour时没有任何错误。

1 个答案:

答案 0 :(得分:2)

一切似乎都是正确的。如果仍无法正常工作,请尝试以下代码段:

    public function includes()
{
    return $this->belongsToMany('App\Included','included_tour', 'tour_id', 'included_id');
}