In this lesson, we will discuss UUID and ULID and how to use them in Laravel.
UUID stands for Universally Unique Identifier, and ULID stands for Universally Unique Lexicographically sortable Identifier. These are not Laravel or PHP terms, their application is broader. It's a long sequence of digits and letters to encode a more extended identifier instead of an ID field.
Now, why would you need that?
The most typical example is when you want to show a user's profile but hide the ID from the URL. For that, you may want to use a UUID.
How to use UUIDs within Laravel? First, we must create a UUID DB column.
database/migrations/xxx_add_uuid_to_users_table.php:
Schema::table('users', function (Blueprint $table) { $table->uuid();});
In my opinion, you should add your UUID in addition to the primary ID, not instead of ID. It would help if you had both and could query from each. Because ID is faster, you would have fewer performance issues. And you would use UUID only for some cases where you want to hide that ID visually.
Next, we must add the HasUuids
trait to the Model.
app/Models/User.php:
use Illuminate\Database\Eloquent\Concerns\HasUuids; class User extends Authenticatable{ use HasFactory, Notifiable; use HasUuids; // ...}
If you use the UUID column alongside the ID column, you must tell Laravel which column to use in the uniqueIds()
method.
app/Models/User.php:
class User extends Authenticatable{ // ... public function uniqueIds(): array { return ['uuid']; }}
Now, when a new user is created, it will also have a UUID. But if you use the UUID alongside the ID column, you must change the route key to be able to use Route Model Binding.
app/Models/User.php:
class User extends Authenticatable{ // ... public function getRouteKeyName(): string { return 'uuid'; }}
In the URL, the UUID can now be used instead of ID.
If you want to read more about UUID, we have some articles:
The main difference between UUID and ULID is how they are generated. UUID is generated using a random number generator, while ULID is generated using a timestamp and a random number generator. This means that ULID is sortable or auto-incremental, while UUID is not.
Similar to UUID, to use a ULID in the Model, you must use a trait HasUlids
.
app/Models/User.php:
use Illuminate\Database\Eloquent\Concerns\HasUlids; class User extends Authenticatable{ use HasFactory, Notifiable; use HasUlids; // ...}
And add a field to the table.
database/migrations/xxx_create_users_table.php:
Schema::create('users', function (Blueprint $table) { $table->id(); $table->ulid(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps();});
If you use the ULID column alongside the ID column, you must tell Laravel which column to use in the uniqueIds()
method.
app/Models/User.php:
class User extends Authenticatable{ // ... public function uniqueIds(): array { return ['ulid']; }}
The same is true for the route key, which is required to use Route Model Binding.
app/Models/User.php:
class User extends Authenticatable{ // ... public function getRouteKeyName(): string { return 'ulid'; }}
The ULID is now used in the URL.