How to use UUID as Primary key instead of auto-increment IDs in Laravel application?
Vložit
- čas přidán 31. 07. 2024
- What is UUID? UUIDs stands for Universally Unique Identifiers, used to uniquely identify the table records. In this, you will learn how to replace Primary key auto-increment IDs with UUIDs. And you will also learn the advantages and disadvantages of using UUIDs in your database.
#Laravel #UUIDs
⌚ Timestamps:
00:00 Introduction
00:59 Create Fresh Laravel APP
02:13 Create Post Model & Migration
03:39 Remove auto-increment IDs
05:39 Add UUID as Primary key
07:41 Auto-generate UUID when creating a post
11:57 Advantages and disadvantages of using UUIDs
▶ Laravel Tips and Tricks
• Laravel Tips and Tricks
▶ Laravel Model Lifecycle Events and How to use Observers in Laravel?
• Laravel Model Lifecycl...
▶ #1: SPA Authentication using Laravel Sanctum
• #1: SPA Authentication...
▶ #2: API Token Authentication using Laravel Sanctum
• #2: API Token Authenti...
▶ Laravel Eloquent Relationships
• Laravel Eloquent Relat...
▶ Impersonating Users in Laravel And Example of TDD using PHPUnit
• Impersonating Users in...
▶ Laravel Macros - How to create a Macro? How to create a class-based macro using mixin?
• Laravel Macros - How t...
▶ Laravel Packages
• Laravel Packages
▶ Laravel Backup Tutorial
• Laravel Backup Tutorial
▶ Laravel Fortify Tutorial
• Laravel Fortify Tutorial
▶ Laravel Passport Tutorials
• Laravel Passport Tutor...
▶ Setup Web Development Environment Ubuntu
• Setup Web Development ...
Support my work:
1. On BuyMeACoffee: www.buymeacoffee.com/qirolab
2. On Patreon: / qirolab
𝐃𝐢𝐠𝐢𝐭𝐚𝐥𝐎𝐜𝐞𝐚𝐧 𝐑𝐞𝐟𝐞𝐫𝐫𝐚𝐥
m.do.co/c/e740238537d0
Also, follow us on:
𝐅𝐚𝐜𝐞𝐛𝐨𝐨𝐤: qirolab
𝐓𝐰𝐢𝐭𝐭𝐞𝐫: / qirolab - Věda a technologie
⌚ Timestamps:
00:00 Introduction
00:59 Create Fresh Laravel APP
02:13 Create Post Model & Migration
03:39 Remove auto-increment IDs
05:39 Add UUID as Primary key
07:41 Auto-generate UUID when creating a post
11:57 Advantages and disadvantages of using UUIDs
Thanks for taking the time to do put the video together. I think you should add what do to if you have a live application and would want to change or "convert" the id field to a UUID field.
Thank you for video. Especially HasUuid trait trick was very helpful :) But if we delete the id field (which is auto incrementing), rows in the table will be not sorted because primary key (uuid) is a char field. To avoid that I recommend you guys to keep id field as it is and add uuid field as additional (instead of removing id field) and give it unique attribute. Then in your model, only use the HasUuid trait and add following part of code for route model binding and remove other declarations of variables in this video (such as $primaryKey);
public function getRouteKeyName()
{
return 'uuid';
}
In this way you gonna have id and uuid fields at the same time. id still will be primary key and auto incrementing, but because of route model binding feature, you will be able to use uuid's in links.
I appreciate this, Good info. Thanks for sharing your thoughts.
Best video of uuid !! thanks!!
Thanks
Thank you so much. You explained very easily.
You are welcome!
Muchas gracias amigo! Buena explicación
Thank you 😊
thanks so much sir
This is an old video, but as a complementary, instead of using a cycle listener to generate the UUID at Post creation, we could just do it from the native DB function from the migration itself: $table->uuid('uuid')->index()->default(DB::raw('(UUID())'));
As well as making this column as an index.
Thanks.
I didn't know about it thanks for sharing 1 more change which is casting because how you can get the id of newly created record like $product->id solution is casting. and what about your thoughts if we drop "primary id auto increment"? according to my experiment never drop primary id, instead make 2nd column called uuid or external_id of uuid dattype char(36). I know its not because of db storage but think about if any primary_id delete suppose 5, then new record id will be 6 but in case of uuid you can use the uuid of delete row because uuid is not set primary. Even if we set uuid column as primary with auto_increment off then we can face same issue.
amazing videos. Thanks for sharing it :)
Glad you like them!
savior. Tahnk you
Glad you like it!
Thanks 😊
You're welcome!
Amazing Video! Very interesting topic. Keep going!
Can I know what is your terminal and Vs theme?
Thanks
i do have table user and profile, does both of them need uuid or only profile ?
very nice
Nice and easy to follow video, thanks - do you have a video how you configured your visual studio code for laravel development?
Not yet! I will create video on this as well.
thank you
Welcome
Lol
love you 3000
Thank you so much
Instead of a trait, I prefer to create a parent model with $incrementing, $primaryKey, and $keyType declarations and a static boot function, that assigns uuid to the primary key (Str::uuid()->toString()). After that, it is enough to inherit this parent model :)
That is also a good solution.
wawwwwwwwwwwww thanks
Glad you like them!
Need help!!!! take 1hour to import 50k records laravel matwebsite package what to do??THanks
You should use Laravel queue for that. I will create video on this as well.
Hiii sir
Hello
I ask a question about
Crack APK
UUID should be string and not class. Replace `Str::uuid()` to `Str::uuid()->toString()`
for the love of God - use shortcuts so u dont have to type full php artisan migrate etc instead a:m or something else.. we dont need to see you type the whole thing - applies to everyone not just this video