我有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
时没有任何错误。
答案 0 :(得分:2)
一切似乎都是正确的。如果仍无法正常工作,请尝试以下代码段:
public function includes()
{
return $this->belongsToMany('App\Included','included_tour', 'tour_id', 'included_id');
}