How to use UUID as Primary key instead of auto-increment IDs in Laravel application?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello everyone welcome to our channel my name is harish kumar this video is the part of the laravel tips playlist i will put the link of it in the video description you should check out this playlist as well today in this video i am going to show you in laravel how you can remove or modify the primary key for the table for example let's say for primary key you want to use uuid instead of auto increment id column i will also explain what is uuid and what are the advantages and disadvantages of uuid before moving ahead a quick reminder if you are new to this channel hit the red subscribe button and press bell icon so when i upload new videos you get notified now let's start so first i'm going to create a fresh new laravel app so laravel new let's say app name is laravel app now app is created next cd laravel app and get in it get add git commit initially now let's open this in the editor next in the dot env file i will configure the database so here username is going to be hero lab password is password next i will create this laravel app database so in the terminal valid db create loadable app in case if you also want to learn how to setup valet in ubuntu system i have already created a video on this you can find the link of it in the video description now database is created so php artisan migrate and it has migrated successfully next i am going to create a post model so php artisan make model post and m flag for migration and migration is created successfully so in the editor let's open the database migrations here is the post table migration and here i am going to add table string title now let's migrate hp artisan migrate and in the database here we have post table now next let's run the php artisan tinker and here i'll create new post record so post create and let's pass title and let's say title is post title and here i get mass assignment exception so let's fix this and so in the app models here the post model and here i'll say model fillable and in this field level we have title so next exit this tinker and run tinker again and create this post again and here we go the post is created and in the database in the post table we have new post record now let's say in your application you have some kind of table where you do not want to use primary key id column so how you can remove that id column from the table and of course it is very easy in the table migration we will just remove this table id column so let's comment this and then the migration again so exit php artisan my create fresh php artisan tinker and now create a post again and here you will see that the post is created but we have this id column and in the database in the post table we have new record without id column but right here it has given this id no matter how many times we create a post it will always give this id so how we can remove this from here so if we go to the host model and if we see this model class and here we have primary key that is id so i'm going to copy this and paste that in the post model and here i'll say this primary key is null and now let's go to the terminal and exit this tinker and and open this tinker again now let's create a post and here we get this error that error is because if we see this model class again here we have another property that is incrementing true and this incrementing option is for id columns for auto increment so here i am going to copy this and paste that here and set this true force now let's reopen this tinker and create post model again and this time you will see that the id column is no longer present here we have successfully removed the auto increment primary key from the post table now let's say you want a primary key but instead of id column you want to use uuid so for that here we will add a new column table uu id and its name is going to be uuid and it is unique column now to use this uuid as primary key in the post model here in the primary key we will pass uuid now you may ask that what is uuid it is a universal unique id which can be used as primary key in the database next in the model class here you will also see that the key type is int that is the type of primary key id because here we are using uuid so in that case this is no longer integer value so here i'll copy this and paste that here i will set this key type string now in the terminal let's create a new post so so let's exit this tinker and first let's migrate our database so php artisan my create fresh and php artisan tinker let's create a post this time we need to pass uuid as well so to generate a uuid laravel providers helper function str uuid to generate universal unique id now let's enter and here we get error uuid doesn't have default value that is because here we need to pass this uid column in the in the fillables like this and run the tinker again and create post the post is created with this universal unique id now here we do not want to pass uid every time when we are creating post and we also do not want to pass uuid in the availables so let's remove it from here now how we can generate this queue id every time when we are creating a post for that we will use laravel model events if you do not know about laravel model events i have already created a video on this in that video i have explained in detail about laravel observers and model life cycle events you must check out that video also you can find the link of it in the video description now let's generate this uu id using laravel events in laravel there are few different methods to use model events here i am going to use boot method so here in this post model i am going to create protected static function boot and here i'll call parent boot and now here in this boot method we can use different model events now to generate a uuid i am going to use creating event so here i will say static creating and here i will pass a closure function and this function going to accept model this model is a post model now here i'll check if dollar model uuid is empty so let's use the empty function in that case here i'll say dollar model uuid is equal to sgr uid function and make sure to import this class this eliminate support hdr here it is now it's done let's go to the terminal and rerun this tinker now let's create a post and this time i'm not going to pass this u id and here was the syntax error and this square bracket was missing and here we go it has created universal unique id automatically in the model class here we can also see a function get key name that returns a primary key so right here instead of using this uuid directly we can say in the curly bracket dollar model get key name like this and use this right here also now let's see if it works and create a new post and it worked it has created a unique id because in other tables can also have uuid column so i am going to create a new trade which can be reused in other models as well so so here i'll create a new folder trades and in this folder i am going to create a new file has uuid dot php and here i'll create trade as uuid and here i'm going to add namespace app model traits like this and next i'm going to copy this boot function from here and paste that right here and make sure to import this class and remember that when you are using this boot in the trade add this straight name after boot like this and we no longer need this parent boot and now here we can say use has you id trade now let's see this so exit run the tinker again and create post and it has created a post with uuid now you may ask that why we will use uuid what are the advantages of this let's say your application is on a distributed system which has multiple databases so in database a in the table x the auto incremented ids are going to be 1 2 3 4 and so on and also in database b table x the auto incremented ids can be 1 2 3 4 5 and so on so this can be a huge problem for your application so to solve this problem we use uuid which is unique across every table every database and every server so the main advantage of this uuid is it allows easy distribution of databases across multiple servers it also have disadvantages the first disadvantage is that its size is 4 times larger than the normal 4 byte index value and because it looks ugly so it is little harder to do manual debugging it also drops the index performance as well so if you have single database then use auto increment id but if you are working with the multiple database then it is better to use youtube id this is all in this lesson thanks for watching if you like the video hit the like button share this video and don't forget to subscribe us see you in the next lesson you
Info
Channel: QiroLab
Views: 18,285
Rating: undefined out of 5
Keywords: laravel uuid as primary key, laravel uuid as id, laravel 8 use uuid as primary key, laravel uuid column, laravel model create uuid, laravel generate uuid on create, laravel uuid default value, uuid database, uuid eloquent, uuid instead of id, uuid vs id, learn with qirolab, qirolab
Id: 1XIMI5kOPuc
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Thu Jun 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.