Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tr3x's avatar
Level 1

Duplicate entry for key 'users_email_unique'

Dear,

I'm trying to import a CSV of users. However when i run the import I get the error shown below. There is only one user which is myself and the mailadress is not a duplicate or anything like that. Is there anyone who could point me in the right direction please ?

Error: Import failed: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique'

CSV file: Voornaam,Achternaam,Email,Compagnie,Post,Function Some,User,[email protected],1,PN,Coolguy

UsersImport.php:

namespace App\Imports;

use App\Models\User; use Illuminate\Support\Facades\Hash; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithBatchInserts; use Maatwebsite\Excel\Concerns\WithChunkReading; use Maatwebsite\Excel\Concerns\WithHeadingRow;

class UsersImport implements ToModel, WithHeadingRow, WithBatchInserts, WithChunkReading {

public function model(array $row)
{
    // Zoek een bestaande gebruiker op e-mail
    $user = User::where('email', $row['email'])->first();

    if ($user) {
        // Als de gebruiker bestaat, update de naam
        $user->update([
            'name' => $row['voornaam'] . ' ' . $row['achternaam'],
        ]);
    } else {
        // Als de gebruiker niet bestaat, maak een nieuwe aan
        $user = User::create([
            'name' => $row['voornaam'] . ' ' . $row['achternaam'],
            'email' => $row['email'],
            'password' => Hash::make('defaultpassword'),
        ]);
    }

    \Log::info('Importing user:', [
        'email' => $row['email'],
        'exists' => $user ? true : false,
        'name' => $row['voornaam'] . ' ' . $row['achternaam']
    ]);

    return $user;
}

public function batchSize(): int
{
    return 100; // Batches van 100 records
}

public function chunkSize(): int
{
    return 100;
}

}

0 likes
7 replies
Snapey's avatar

The answer is, don't repeat the same email. Do you have this email address more than once in the import file?

tr3x's avatar
Level 1

The emailadress is not in the database nor is it multiple times in the CSV. The only user in this database is myself with my gmail.com account.

martinbean's avatar

The emailadress is not in the database

@tr3x

Doubt

Then why would you get an error saying there’s a duplicate entry for the email address if it’s not there? 🤷‍♂️

Error messages are seldom wrong. If it’s saying there’s a duplicate entry, then there’s an entry with that email address that exists already.

tykus's avatar
tykus
Best Answer
Level 104

You've updated the question since my previous reply; I see now you are using the toModel method incorrectly, it should not be creating a Model instance, per docs:

When using ToModel you should never save the model yourself, as that will break the batch insert functionality. If you need this, consider using OnEachRow.

In fact, you should probably consider the Upserting Models instead!

1 like
tr3x's avatar
Level 1

@tykus Thanks for pointing this out! Fixed it with the upserts and all seems to be working well now! Thanks for pointing me in the right direction!

@martinbean well i was cetrain about the mailadress, but the above solution did it for me.

martinbean's avatar

@tr3x Well that’s what happens when you get an error, and instead of showing the actual code you make up pseudo-code and hide the error 🙃 And that my statement would have been correct: that if you were getting an error saying the address already existed, then the address did indeed already exist.

Please share the actual problem code in the future instead of saying “I’ve got an error with this code” but then making up a completely different code example. You’re just wasting everyone’s time if you make them try and find a bug in code that isn’t even the actual problem code.

Please or to participate in this conversation.