2
votes
User::updateOrCreate([
    'identifier' => $user->id,
    'username' => $user->nickname,
    'name' => $user->name,
    'avatar' => $user->avatar,
    'visibility' => $user->visibility,
    'api_token' => Uuid::generate()
]);

And my migration:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('identifier')->unique();
    $table->string('username')->unique();
    $table->string('name');
    $table->string('avatar');
    $table->string('trade')->nullable();
    $table->decimal('funds')->default(0);
    $table->enum('visibility', [1, 2, 3]);
    $table->uuid('api_token');
    $table->timestamps();
});

But results in a QueryException :(

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '76561198364059468' for key 'users_identifier_unique'

2
the error is pretty much self-explanatory. You may want to use INSERT ... ON DUPLICATE KEY UPDATE dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html - Funk Forty Niner

2 Answers

0
votes

This error says that there is a row in the users table where identifier has value of 76561198364059468 already. So just use an unique identifier to avoid the error.

0
votes

Do like this

User::updateOrCreate(
    [
        'identifier' => $user->id
    ],
    [
        'username' => $user->nickname,
        'name' => $user->name,
        'avatar' => $user->avatar,
        'visibility' => $user->visibility,
        'api_token' => Uuid::generate()
    ]
);