First published on my website
What are UUIDs?
UUID stands for Universal Unique Identifier. It's a 128-bit number used to uniquely ident...
For further actions, you may consider blocking this person and/or reporting abuse
I am getting issue with
sync()
method.I did somthing like:
then i got this error:
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();
}
});
}
Wilbur Powery
Great post Wilbur, thank you. I think you can also just set
public $incrementing = false;
, which saves a few lines.Thanks Owen. You're right, setting that to false is much easier, thanks for pointing it out.
Using public $incrementing = false; doesn't work as it gives you a dual declaration if you use the trait - stick to getIncrementing
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.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
Great post, I didn't know that it was so easy! I am running into a problem with my foreign keys:
Any idea on what I'm doing wrong?
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
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.
It isn't working for me. it shows the following error, "Trait 'App\Concerns\UsesUuid' not found"
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.
Thanks for this, I've come back to this article two or three times in my career now. 😅
That Trait does NOT work in Laravel 8
how to generate with laravel8
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
In this way, is it make sure that UUID's will be unique?
Great.
How to generate 12 digit uuid like medium does ?
Thanks this works so perfectly!
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..