Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

UUID in Laravel: All You Need To Know

December 08, 2022
9 min read

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.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

B
Boran ✓ Link copied!

Hello Povilas,

In the article you say:

Then, the new Laravel 9.30 way doesn't apply because it works only for primary keys

But in the Laravel documentation it says:

In addition, you may specify which columns should receive UUIDs by defining a uniqueIds method on the model

/**
 * Get the columns that should receive a unique identifier.
 *
 * @return array
 */
public function uniqueIds()
{
    return ['id', 'discount_code'];
}

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?

LN
Loganathan Natarajan ✓ Link copied!

Thanks for the detailed information with example. It will be useful for larger projects to use.

AA
Ali Awwad ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

I think both options may work.

AA
Ali Awwad ✓ Link copied!

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.

S
Sjoerd24 ✓ Link copied!

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.

M
Modestas ✓ Link copied!

I'm not sure to be fair. But if you have set route key name - it should be separate from relationship. Give it a try if you have a working system and it will show!

S
Sjoerd24 ✓ Link copied!

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)