I am using a user verification system in email with Laravel 5.7 this is working fine. but when I tried to enter the same email with verification form it is generating following massages.
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'mymail@gmail.com' for key 'users_email_unique' (SQL: insert into `users` (`email`, `name`, `address`, `updated_at`, `created_at`) values (mymail@gmail.com, Banda, Sysney, 2019-05-31 11:32:36, 2019-05-31 11:32:36))
my UserController is like this,
protected function store(Request $request)
{
$user = new User;
$user->email = $request->input('email');
$user->name = $request->input('name');
$user->address = $request->input('address');
$user->save();
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
}
how can fix this problem?
答案 0 :(得分:0)
似乎并且也很常见,您的users
表似乎在email
上具有UNIQUE INDEX,因此,如果您尝试使用已存在的电子邮件创建条目,则获取这个错误。
我不确定您要从问题上下文中确切地做什么,但是我相信如果您只想创建一个VerifyUser条目,则可以使用updateOrCreate()
作为User并继续执行VerifyUser逻辑,如下所示:< / p>
protected function store(Request $request)
{
$email = $request->input('email');
$name = $request->input('name');
$address = $request->input('address');
$user = User::updateOrCreate(['email' => $email], ['name' => $name, 'address' => $address]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
}
我希望对您有帮助