Skip to content

Instantly share code, notes, and snippets.

@harveydobson
Last active March 14, 2024 22:47
Show Gist options
  • Save harveydobson/1c6c4622ead5b2548bc0be2d9fc7041b to your computer and use it in GitHub Desktop.
Save harveydobson/1c6c4622ead5b2548bc0be2d9fc7041b to your computer and use it in GitHub Desktop.
Using UUIDs for user ids in Laravel Nova

Using UUIDs for user ids in Laravel Nova

I recently created a project where I wanted my user's to be represented with a uuid.

UUID v7 allows for a balanced option between randomly generated, unpredictable user IDs, and sequential IDs.

The first few characters of a UUID v7 are based on the current timestamp, allowing for the IDs to be sorted.

This is ideal I thought...

First I modified my user table migration

Schema::create('users', function (Blueprint $table) {
    $table->uuid('id')->primary();
    ...
}

This worked a treat, I was now able to set uuids as IDs.

But when I tried to log into Laravel Nova, I found I was not getting any error, but being redircted back from the dashboard.

After much investigation and finding no answers on a google search, I realised this was my issue.

If you want to use a uuid in your user model (or any model for that matter) you should add the following fields to your User model:

public $incrementing = false;
protected $keyType = 'string';

Further to this, if you are using Laravel Passport, you will want to override this function too:

public function findForPassport($username) {
return $this->where('id', $username)->first();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment