# SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

MySQL refused an insert because the value already exists in a unique column. Validate first, use firstOrCreate or upsert, and catch the exception for races.

Message:

```text
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ada@example.com' for key 'users.users_email_unique'
```

Exception:

```text
Illuminate\Database\UniqueConstraintViolationException
```

## What it means

The table has a unique index, and the insert or update would have put a second row with the same value in it. MySQL refused the statement. The message names the value and the index, here `users_email_unique` on the `users` table. Laravel throws `Illuminate\Database\UniqueConstraintViolationException`, a subclass of `QueryException`, so a `catch (QueryException $e)` also catches it.

## Why it happens

- Nothing checks for an existing row before the insert. A second signup with the same email reaches `create()` and the index stops it.
- Two requests race past the `unique` validation rule. The rule runs a query; it does not hold a lock. Two requests that both validate before either one inserts both pass, and the slower insert fails. Of two signups with the same email sent at the same moment, one signup succeeded and the other got a 500.
- A seeder or import runs twice. The default `DatabaseSeeder` creates `test@example.com`, so a second `php artisan db:seed` fails with this error on that address.

## How to fix it

Validate the input so the visitor gets a form error instead of a 500:

```php
$validated = $request->validate([
    'email' => ['required', 'email', 'unique:users,email'],
]);
```

On an edit form, let the record keep its own value. The validation docs warn to pass only a system generated id to `ignore`, never request input:

```php
use Illuminate\Validation\Rule;

$rules = [
    'email' => ['required', Rule::unique('users')->ignore($user->id)],
];
```

When finding the existing row is fine, let Eloquent do it. `firstOrCreate` returns the row that matches the first array, or creates it with both arrays merged. `updateOrCreate` also updates the existing row:

```php
$user = User::firstOrCreate(
    ['email' => $email],
    ['name' => $name, 'password' => $password],
);
```

For seeders and imports, `upsert` inserts or updates in one statement. The docs note that MySQL ignores the `uniqueBy` argument and always uses the table's primary and unique indexes:

```php
User::upsert(
    [['email' => 'ada@example.com', 'name' => 'Ada', 'password' => $hash]],
    uniqueBy: ['email'],
    update: ['name'],
);
```

Validation cannot close the race; the unique index is what does. In Laravel 13, `firstOrCreate` already handles it: when its insert hits the index, it reads the row back instead of throwing. For a plain `create`, catch the specific exception and answer as if validation had failed. `$e->index` names the index that stopped the insert, so rethrow when it is another one:

```php
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Validation\ValidationException;

try {
    $user = User::create($validated);
} catch (UniqueConstraintViolationException $e) {
    if ($e->index !== 'users_email_unique') {
        throw $e;
    }

    throw ValidationException::withMessages([
        'email' => 'This email address is already registered.',
    ]);
}
```

## Find it in production

With `APP_DEBUG=false` the visitor gets a 500 page titled "Server Error", usually after submitting a form. Laravel reports the exception, so each one is written at error level to `storage/logs/laravel.log` with the duplicate value in the message. The message also holds the insert SQL with every value filled in, password hash included, unless `DB_MASK_BINDINGS=true` is set and your `config/database.php` connection passes it on as `mask_bindings_in_exception_messages`. Ranetrace's [error monitoring](/features/error-monitoring) records each one with its stack trace and the request it happened on.

Reproduced on Laravel 13.45, PHP 8.5, MySQL 8.0.27, 5 December 2026.

Read more: https://laravel.com/docs/validation

## Related

### Terms

- [Validation rules in Laravel](https://ranetrace.com/glossary/validation-rules-in-laravel.md)
- [Eloquent ORM in Laravel](https://ranetrace.com/glossary/eloquent-orm-in-laravel.md)
- [Seeders in Laravel](https://ranetrace.com/glossary/seeder-in-laravel.md)

### Other errors

- [Add \[x\] to fillable property to allow mass assignment](https://ranetrace.com/error-guides/laravel-mass-assignment-fillable.md)
- [SQLSTATE\[42S22\]: Column not found: 1054 Unknown column](https://ranetrace.com/error-guides/laravel-sqlstate-1054-unknown-column.md)
- [SQLSTATE\[23000\]: Integrity constraint violation: 1452 Cannot add or update a child row](https://ranetrace.com/error-guides/laravel-sqlstate-1452-foreign-key-constraint.md)

Read this guide in a browser: https://ranetrace.com/error-guides/laravel-sqlstate-1062-duplicate-entry
