Laravel has a Str::orderedUuid()
method that the trait HasUuids
uses by default. I understand that uuids from this method can be sorted lexicographically and it helps databases indexation.
https://laravel.com/docs/11.x/eloquent#uuid-and-ulid-keys
By default, The HasUuids trait will generate "ordered" UUIDs for your models. These UUIDs are more efficient for indexed database storage because they can be sorted lexicographically.
Why Laravel does not use UUID v7 (e.g. Str::uuid7()
) which is also timestamp based and can be can be sorted lexicographically based on my understanding of the Symfony documentation about UUIDs ?
2
Answers
I don’t know which version you are using but
Laravel-11
use it. You need to modify your model to use theHasVersion7Uuids
trait. This trait will automatically generate aversion-7 UUID
when a new model is created.You need to import and use this trait
IlluminateDatabaseEloquentConcernsHasVersion7Uuids;
.In that trait thisnewUniqueId()
method generate and retunr a new UUID (version 7) for the model.For detailed information check this
Laravel-11
API documentation link.Also check this link.
Laravel’s
Str::orderedUuid()
function has existed since 2018(Laravel v5.6) originally trying to fix problems with UUID v4. More details onStr::orderedUuid
implementation can be found in this blog post.In contrast, UUID v7 is also relatively recently introduced to reach similar goals. But keep in mind that UUID v7 has been made a standard only this year with the publication of RFC 9562 in May 2024.
So to answer your question on why Laravel
HasUuids
preferred its own implementation could simply be that it predates v7 becoming the standard.Like always to still give you flexibility, Laravel v11 gives you the trait
IlluminateDatabaseEloquentConcernsHasVersion7Uuids
as another answer also suggested which lets you use the new standard without breaking existing code bases.I would like you to note that both are timestamp-based and both are lexicographically ordered and for general use cases are similar in use, the difference being one is a standard now and the other is not.