sylvaindeloux's avatar

Session issue when using UUID instead of auto-incremented ids on User

Hi everyone,

I'm trying to use UUIDs instead of ids on users table, it works fine but I can't login anymore, session is not created.

First I added a HasUuid trait (and use it in App\Models\User):

trait HasUuid
{
    public static function booted(): void
    {
        parent::boot();

        static::creating(function (Model $model): void {
            $model->setAttribute('uuid', Str::orderedUuid());
        });
    }
}

Then I added these properties to my User class:

protected $primaryKey = 'uuid';
protected $keyType = 'uuid';
public $incrementing = false;

Of course, migrations has been updated:

  • User migration : from$table->id(); to $table->uuid()->primary();
  • Sessions migration : from $table->foreignId('user_id')->nullable()->index(); to $table->foreignUuid('user_id')->nullable()->index();

The issue is the same when I use other session storage. What did I miss?

Thanks :)

1 like
1 reply
Sinnbeck's avatar

I believe you need to set the key type to string (unless it was recently changed)

protected $keyType = 'string';

Please or to participate in this conversation.