...
use Illuminate\Database\Eloquent\Concerns\HasUlids; <- ADD
...
class User extends Authenticatable
{
...
use HasUlids; <- ADD
...
}
User migration
Change from type 'id' (alias of 'bigIncrements'), an auto-incrementing big integer) to 'ulid' and set it as the primary key.
{
...
// $table->id(); <- CHANGE FROM
$table->ulid('id')->primary(); <- CHANGE TO
...
}
Session migration
As the user 'id' is no longer an auto-incrementing integer, we need to update the session table from 'foreignId' (alias for UNSIGNED BIGINT) to 'foreignUlid'.
{
...
//$table->foreignId('user_id')->nullable()->index(); <- CHANGE FROM
$table->foreignUlid('user_id')->nullable()->index(); <- CHANGE TO
...
}
To use ULID or UUID as the primary key for the User model when using Jetstream, you need to follow the steps mentioned in the question description. Here's a summary of the steps:
Add the HasUlids trait to the User model:
use Illuminate\Database\Eloquent\Concerns\HasUlids;
class User extends Authenticatable
{
use HasUlids;
...
}
Change the id column in the User migration to use ulid and set it as the primary key:
Schema::create('users', function (Blueprint $table) {
$table->ulid('id')->primary();
...
});
Change the user_id column in the session migration to use foreignUlid:
Schema::create('sessions', function (Blueprint $table) {
$table->foreignUlid('user_id')->nullable()->index();
...
});
That's it! Now you can use ULID or UUID as the primary key for the User model when using Jetstream.