Reputation: 402
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 '[email protected]' for key 'users_email_unique' (SQL: insert into `users` (`email`, `name`, `address`, `updated_at`, `created_at`) values ([email protected], 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?
Upvotes: 1
Views: 1059
Reputation: 12277
As it seems and also very common, your users
table seems to have a UNIQUE INDEX on email
so if you try to create an entry with an email which already exists, it is normal to get this error.
I am not sure what exactly you want to do from your question context but I believe if you just want to create a VerifyUser entry, you could use updateOrCreate()
for User and go on with VerifyUser logic like so:
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)
]);
}
I hope it helps
Upvotes: 0