我正在尝试同步数据,但发生错误。
SQLSTATE[HY000]:常规错误:1364 字段"model_type"没有默认值(SQL:插入到
model_has_roles
(dealer
、dealership_id
、model_id
、role_id
(值(1、61、85、13((
控制器代码
$user = User::findOrFail($id);
$user->first_name = $request->get('first_name');
$user->last_name = $request->get('last_name');
$user->phone = $request->get('phone_no');
$user->title = $request->get('title');
$user->mobile = $request->get('mobile');
$user->save();
$user->dealerRoles()->wherePivot('dealership_id', session('dealership_id'))->sync([$request->get('role_id') => ['dealer' => true, 'dealership_id' => session('dealership_id')]]);
用户模型
public function dealerRoles()
{
return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id');
}
您需要添加到同步方法
$user->dealerRoles()
->wherePivot('dealership_id', session('dealership_id'))
->sync([
$request->get('role_id') => [
'model_type' => 1,
'dealer' => true,
'dealership_id' => session('dealership_id')
]
]);