嗨,我在laravel非常新,我偶然发现一些麻烦将我的价值传递给我的输入,下面是代码:
$user = User::updateOrCreate([
'username' => $request->input('username'),
'password' => $hasher->make($request->input('password')),
'costumer_id' => $request->input('costumer_id'),
'role_id' => $request->input('role_id')
]);
$costumer= Costumer::updateOrCreate([
'id' => $user->costumer_id,
'email' => $request->input('email'),
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'phone_number' => $request->input('phone_number'),
]);
$register = Sentinel::registerAndActivate($request->all());
我想将我刚输入的costumer_id传递给客户表中的我的ID,但我收到错误Foreign key violation: 7 ERROR: insert or update on table "users" violates foreign key constraint "users_costumers_id_foreign"
有人可以帮助我,我该怎么做?提前谢谢
答案 0 :(得分:0)
$user = User::updateOrCreate([
'username' => $request->input('username'),
'password' => $hasher->make($request->input('password')),
'costumer_id' => $request->input('costumer_id'),
'role_id' => $request->input('role_id')
]);
if($user){
$costumer= Costumer::updateOrCreate([
'id' => $user->costumer_id,
'email' => $request->input('email'),
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'phone_number' => $request->input('phone_number'),
]);
}
$register = Sentinel::registerAndActivate($request->all());
或者您可以使用交易。这是更好的选择。