我建立了Film和Performer模型之间的关系。
薄膜模型:
public function Performers()
{
return $this->belongsToMany('App\Performer');
}执行者模型:
public function Films()
{
return $this->belongsToMany('App\Films');
}如果要手动添加数据,我已经创建了表film_performer和relationship。
在发送表单后将数据保存到film_performer中有问题。
我把它附在控制器上。
$film->performers()->attach($request->performers);我没有任何错误。我使用dd($request)来检查数组表演者,一切正常。
发布于 2018-02-01 08:05:07
将$request->performers更改为$request->performers->pluck('id'):
$film->performers()->attach($request->performers)attach方法需要一个ids数组。
https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships
为了方便起见,附加和分离还接受ID数组作为输入: $user = App\User::find(1); $user->->detach()角色(1,2,3); $user->->attach()角色([1 =>‘过期的’=> $expires,2 =>‘到期的’=> $expires ]);
https://stackoverflow.com/questions/48557936
复制相似问题