PHPWarningDatabase ErrorJune 14, 2026

Database Error

Uncaught Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user123' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `password`) values (?, ?, ?))'

What This Error Means

This error occurs when attempting to insert a new record into a database table with a unique key constraint that is already violated. In this case, the 'email' field is a unique key and another record with the same 'email' already exists.

Why It Happens

This error can happen in Laravel's Eloquent ORM when calling the `create` method on a model, which by default will automatically insert a new record into the database. If the model is set up to enforce unique keys, and a duplicate record is inserted, this error will be thrown.

How to Fix It

  1. 1To fix this error, you need to either update the existing record with the duplicate 'email' or create a new 'email' that is unique. If using the `create` method, you can use the `updateOrCreate` method instead, which will automatically update the existing record if it exists, or create a new one if it doesn't.

Example Code Solution

❌ Before (problematic code)
PHP
$user = User::create(['name' => 'John Doe', 'email' => 'user123@example.com', 'password' => bcrypt('password123')]);
✅ After (fixed code)
PHP
$user = User::updateOrCreate(['email' => 'user123@example.com'], ['name' => 'John Doe', 'password' => bcrypt('password123')]);

Fix for Uncaught Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user123' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `password`) values (?, ?, ?))'

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error