If you want to replace DB auto-increment IDs with something more sophisticated, one of the solutions is UUID. In this article, I will show you how UUID columns work in Laravel, and what are the options and tools to use them.
What is UUID and Why You Would Need It?
If we take a look at Wikipedia, this is the definition:
"A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is also used."
This term comes not from Laravel or PHP, it's a general concept for IT projects, to uniquely identify records.
Some UUID examples:
- b898564a-4ce8-4114-9067-142b437075ae
- 236d75e7-b7e2-41f4-af8f-7b9e9cf32ed9
- b7c91937-74b2-4230-97cf-92433cc6dd9a
You would save these identifiers in the database, instead of (or in addition to) a typical ID column.
So, if you have these URLs in your project:
- yourproject.com/posts/1
- yourproject.com/transactions/123/edit
With UUID, they would look like this:
- yourproject.com/posts/b898564a-4ce8-4114-9067-142b437075ae
- yourproject.com/transactions/236d75e7-b7e2-41f4-af8f-7b9e9cf32ed9/edit
Why would you do that? A few reasons...
Premium Members Only
This advanced tutorial is available exclusively to Laravel Daily Premium members.
Already a member? Login here
Premium membership includes:
Comments & Discussion
shouldn't app/Traits/Uuid.php contain bootUuid insead of just boot with the removal of parent::boot(); ? because it might result in boot method collision
Using "boot" will only work when you don't have two traits with "boot". For example, the package Venturecraft\Revisionable uses "boot" in its Trait file.
So if you have another Trait also with the same "boot" , such as "Uuid", you will get the following error:
Trait method Venturecraft\Revisionable\RevisionableTrait::boot has not been applied as App\Models\Pass::boot, because of collision with App\Traits\Uuid::boot
So the only option is acutually to name the trait boot method as boot[TraitName].
I could'nt find this documented anyware, but if you take a look at laravel SoftDeletes trait you'll see that it uses the same convention for the naming.
The approach that works with both an id and a uuid seems best to me, but if you specify in the getRouteKeyName a uuid, does that mean if you have a relationship (eg belongs_to) laravel is going to try too use the uuid to find/store the relationship?
Or will it still use the id then? that last option would be perfect because you would retain the speed and simplicity in for example pivot tables.
Edit: ok that's perfect I tested it and (of course, should learn to read better) getRouteKeyName only involves the route, in Filament everything immediatly works perfectly when setting this.
yeah it does work perfectly! Have to say things like this make me love laravel even more, such a deep change but no effort what so ever. In my project I changed 8 tables in 30 minutes. Code above was useful, I just added the getroutekeyname to the trait.
First consider adding a unique key to uuid, but I saw that the chance for a collision is once every 100 years at best (if you create thousands of records per sec)
Hello Povilas,
In the article you say:
But in the Laravel documentation it says:
Source: https://laravel.com/docs/9.x/eloquent#uuid-and-ulid-keys
So can't we use this method to use secondary UUID without making primary key?