我正在尝试在Laravel 5.3中构建一个应用程序,我想在数据透视表中添加其他列数据。以下是我的代码:
我的Users
模特:
public function relations()
{
return $this->belongsToMany('App\Plan')->withPivot('child');
}
我的Plan
模特:
public function relations()
{
return $this->belongsToMany('App\User')->withPivot('child');
}
在我的控制器中,我从Auth::user();
请求中获取计划和子元素的用户数据。我想将其存储到我的数据透视表中。以下是我在控制器中尝试过的代码:
$user = \Auth::user();
$plan_id = $request->plan_id;
$childid = $request->child_id;
$plan = App\Plan::find($plan_id);
$user->relations()->attach($plan, ['child' => $childid]);
帮我解决这个问题。
您应该这样使用attach()
:
$user->relations()->attach($plan_id, ['child' => $childid]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句