UUIDs (Universally Unique Identifiers) have become an essential component of modern web applications, providing a dependable and globally unique method of identifying records in databases.
Generating UUIDs in Laravel 10 provides a solid method for preventing potential conflicts and preserving data integrity, particularly when working in distant and interconnected systems.
Read More: Laravel 10 Read JSON File Example Tutorial
We will walk you through the process of creating UUIDs in Laravel 10 in this article. We will look at various ways and packages that make it easier to generate UUIDs and integrate them into the database structure of your application.
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
What is UUID?
A UUID is a standardised identification that is intended to be unique throughout all space and time. It is a 128-bit value that is commonly expressed as a hexadecimal sequence separated by hyphens.
UUIDs are extensively used in computer systems and software to identify diverse items such as files, database records, network devices, software components, and others. They serve to reduce the likelihood of duplication, making them useful for a wide range of applications where uniqueness is required.
Examples:
93df0150-160a-11ed-a017-15fb2ac449ef
2d01f453-5eb5-4655-b983-8ece09193977
Methods To Generate UUID
In laravel, there are two methods by which you can generate UUID value for use.
- Using Str
- Using Composer package (UUID)
Read More: Laravel 10 MySQL Right Join Example Tutorial
You will find all details of about these methods in detail here.
Generate UUID Using Str
We will use Str class to generate UUID.
Consider this example with a controller file say CustomController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Support\Str; class CustomController extends Controller { public function index() { $uuid = Str::uuid()->toString(); dd($uuid); } }
Concept
You can use toString() and uuid() methods of Str.
$uuid = Str::uuid()->toString();
OR
$uuid = (string) Str::uuid();
Output
93df0150-160a-11ed-a017-15fb2ac449ef
Generate UUID Using Package
Open project into terminal and run this command.
$ composer require webpatser/laravel-uuid
It will install a package. You can use it to generate UUIDs.
Read More: Laravel 10 MySQL Left Join Example Tutorial
Consider this example with a controller file say CustomController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Webpatser\Uuid\Uuid; class CustomController extends Controller { public function index() { $uuid = Uuid::generate()->string; dd($uuid); } }
Concept
You can use generate() method and string property.
$uuid = Uuid::generate()->string;
Output
2d01f453-5eb5-4655-b983-8ece09193977
How to Check a UUID is valid?
Laravel Str class provides a method i.e isUuid. You can use it to check a UUID is valid or not.
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Support\Str; class CustomController extends Controller { public function index() { $isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de'); dd($isUuid); } }
Output
true
We hope this article helped you to learn about Laravel 10 How To Generate UUID Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.