Updated April 10, 2023
Introduction to laravel uuid
Laravel uuid is also known as the universally unique identifier, which was 128 bits value and used to identify the records of the table. UUID is basically used instead of auto-increment ID; UUID is represented as a hexadecimal string which was separated into five groups and splitted by hyphens. We can use the laravel version 7 to use UUID in the laravel application; also, we can use the UUID in other versions, i.e., 5.8 and 6. However, UUID is not generating sequentially like sequence or integer type primary key; the index on primary key is suffering more bloated values.
What is laravel UUID?
- Basically, the laravel UUID is consisted 16-bit values instead of 4 or 8-bit integer values, so UUID requires more space for data storage and indexing.
- UUID’s size is big compared to numeric data type, so UUID requires more RAM to cache the UUID in memory.
- Using integer-type values, we can sort the data easily, but UUID sorting is not easy because UUID generates randomly.
- There is no specific method for UUID sorting; we cannot sort the UUID data because UUID is not in sorting order.
- Below is the sample UUID example are as follows.
127c78de-7dcb-89bc-8767-a43442b9aaab
- In the above sample example, we can see that UUID will be splitted into five different groups.
- We can use create UUID using a generator; we can also use the composer package to generate the laravel unique UUID.
- Also, to generate the UUID in the laravel application, we can also use str façade.
- We can use the UUID in an eloquent model of laravel instead of using the incremental ID’s.
- To use UUID in our application, first, we need to create the laravel application. To use the UUID in an application, we need to use the library of ramsey UUID.
- Ramsey UUID library will help to generate the universally unique identifier in laravel. To use UUID in the existing laravel application, we need to make some changes in the model class.
Using UUIDs in Laravel Models
The below example shows the use of UUID in the model are as follows. To use UUID in the model, we need to follow the below steps.
Create laravel application –
- To use UUID in our laravel application, first, we need to create the application. So, we have to create the application name as uuid.
- For creating the new application, we need to use the create-project parameter. Using create project parameter, we have to create the project.
- After defining the create-project parameter, we have defining the application version.
# composer (composer command is used to create new laravel application) create-project (create new application) --prefer-dist laravel/laravel:^7.0 uuid (Create application name as uuid)
Creating the UUID trait –
- Create the Traits directory under the app directory. After creating the directory, create a new Uuid.php file under the Traits directory.
- To create the Uuid.php file, we are creating the Uuid.php file in the traits directory. We have defined code in the file which was assumes the primary key in an incremental model of an integer value.
- To use the UUID in the laravel application, we must specify the primary key in string value but not in incrementing value.
- We have achieving the same by using the $keyType and $incrementing parameter.
# vi Uuid.php
Update the migration file –
- The third step is to update the migration file. For example, change the UUID instead of ID in the migration file. This is a very important step to update the project migration file.
- In the below application, we have changed in the user class. In addition, we have making the changes in the user migration class.
# vi 2014_10_12_000000_create_users_table.php
Use Traits in our model class –
- The below example shows the use traits in our model class are as follows. In the below example, we need to add the Uuid.php trait in our application model class.
# vi Uuid.php
Using UUID as foreign key –
- The fifth step is to update the migration file. Change the UUID instead of ID in the migration file. In our application, we have using the UUID primary key as a foreign key, so we need to change the column type of this table to UUID, from where we have defined the foreign key.
# vi 2019_08_19_000000_create_failed_jobs_table.php
How to Create Laravel Application?
- The below example shows how to create an application are as follows. We have creating the application name as UUID-laravel.
- In the below example, we have used the create-project command to create a new application name as UUID.
- We have used version as 7 at the time of creating a new application of UUID.
# composer (composer command is used to create new laravel application) create-project (create new application) --prefer-dist laravel/laravel:^7.0 UUID-laravel (Create application name as UUID-laravel)
- In the above project, we have created the project in the root home directory. Because the application is created in the current home directory.
- In the below example, we can see that the application is created in the root home directory and the application directory created inside the application directory.
- In below example, we can see that under UUID directory multiple folder is created.
# cd UUID-laravel/
# ll
Creating a new Laravel app called laravel-UUID
- Below examples, we have created applications are as follows. We have creating the application name as laravel- UUID.
- In the below example, we have used the create-project command to create a new application name as laravel- UUID.
- In the below application, we have created the project in the root home directory.
# composer (composer command is used to create new laravel application) create-project (create new application) --prefer-dist laravel/laravel:^7.0 UUID-laravel (Create application name as laravel- UUID)
- In the below image, we can see that all the application-related files were created under the UUID directory.
# cd laravel-UUID/
# ll
Conclusion
Laravel uuid is known as the universally unique identifier, which was 128 bits value and used to identify the records of the table. UUID is basically used instead of auto increment ID. The size of the UUID is big as compared to the numeric data type.
Recommended Articles
We hope that this EDUCBA information on “laravel uuid” was beneficial to you. You can view EDUCBA’s recommended articles for more information.