DEV Community

Easily use UUIDs in Laravel

Wilbur Powery on October 29, 2018

First published on my website What are UUIDs? UUID stands for Universal Unique Identifier. It's a 128-bit number used to uniquely ident...
Collapse
 
risingad profile image
Rising • Edited

I am getting issue with sync() method.
I did somthing like:

$page->users()->sync([auth()->id() => ['status_id' => 1]]);
Enter fullscreen mode Exit fullscreen mode

then i got this error:

SQLSTATE[HY000]: General error: 1364 Field 'page_id' doesn't have a default value 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bodrosh profile image
Bodrosh

I also encountered this problem, if you want to add a uuid to the table additionally when it already has a key, you can do this:

protected static function bootUsesUuid() {
static::creating(function ($model) {
if (! $model->uuid) {
$model->uuid = (string) Str::uuid();
}
});
}

Collapse
 
risingad profile image
Rising
wilburpowery image
I am using laravel version 5.7
Collapse
 
owenandrews profile image
Owen Andrews

Great post Wilbur, thank you. I think you can also just set public $incrementing = false;, which saves a few lines.

Collapse
 
wilburpowery profile image
Wilbur Powery

Thanks Owen. You're right, setting that to false is much easier, thanks for pointing it out.

Collapse
 
guernseygreen profile image
St Pierre d'Guernsey

Using public $incrementing = false; doesn't work as it gives you a dual declaration if you use the trait - stick to getIncrementing

Thread Thread
 
pstevek profile image
Steve Kamanke

That's only if you use do decide to use a Trait. If you're only using it on only one model then you don't need to create a Trait. public $incrementing = false; would work as intended.

Collapse
 
faridsa profile image
farid

Hi Wilbur, nice point. I'm using phpStan to check my code and the trait fires an error in following line:
$model->{$model->getKeyName()} = (string) Str::uuid();

Error:
Cannot cast Ramsey\Uuid\UuidInterface to string

Collapse
 
slipperydippery profile image
slipperydippery

Great post, I didn't know that it was so easy! I am running into a problem with my foreign keys:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `groups` add constraint `groups_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

Any idea on what I'm doing wrong?

Collapse
 
nwosucc profile image
Nwosu Cyprian

Check that both groups.user_id and users.id have same type definition e.g if users.id is UNSIGNED Integer, groups.user_id should be same

Collapse
 
keithmifsud profile image
Keith Mifsud

Great mini tutorial. Thank you for sharing :)

I would only make one point here regarding Dependency Inversion:

When possible try to generate your UUIDs before your domain/source code and not through the database model. Otherwise, your source is dependant on the database.

Collapse
 
yonasgg profile image
Yonas

It isn't working for me. it shows the following error, "Trait 'App\Concerns\UsesUuid' not found"

Collapse
 
applicationerror profile image
ApplicationError

Thank you! This is really great.

I am having issues with pivot tables though... I'm guessing the Trait will need to somehow ensure that pivot tables also get a uuid.

Collapse
 
taelkir profile image
Tom Stanley

Thanks for this, I've come back to this article two or three times in my career now. 😅

Collapse
 
blade93ny profile image
ZettaV

That Trait does NOT work in Laravel 8

Collapse
 
reakmsey profile image
say reakmsey

how to generate with laravel8

Collapse
 
paulnoris profile image
Paul Noris

Really good post Wilbur, it works good but im having problems using this with seeders and factories, the uuid didn't do the id automatic

Collapse
 
yahyaerturan profile image
Yahya Erturan

In this way, is it make sure that UUID's will be unique?

Collapse
 
tmg_bedus profile image
बेद

Great.

How to generate 12 digit uuid like medium does ?

Collapse
 
massivebrains profile image
Segun Olaiya

Thanks this works so perfectly!

Collapse
 
_zulfiqar_ profile image
zulfiqar875

Hi! Wilbur Power,
if i want to get 8-10 bit integer number ID like (67459823) using UUID generator. then what i need to do..