Laravel 9 How To Generate UUID in Application Tutorial

Reading Time: 4 minutes
8,653 Views

Inside this article we will see the concept i.e Laravel 9 How To Generate UUID in application. Article contains the classified information about generating unique UUID for laravel application. This concept is very useful when you want use a unique uuid.

Article uses the concept of Str (laravel class) and also a composer package to generate unique UUID for application. You will learn the complete step by step concept to generate UUID to use.

What is UUID?

UUIDs are generally used for identifying information that needs to be unique within a system or network thereof. Their uniqueness and low probability in being repeated makes them useful for being associative keys in databases and identifiers for physical hardware within an organization.

A UUID, or a Universally Unique Identifier, is a 128-bit label used for information in computer systems.

Learn More –

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.

Generate Unique UUID Using Str

We will use Str class to generate UUID.

Open a controller use it as –

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
 
class CustomController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $uuid = Str::uuid()->toString();

        dd($uuid);
    }
}

Concept

$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 composer package inside application. We will use it generate UUID.

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Webpatser\Uuid\Uuid;
 
class CustomController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $uuid = Uuid::generate()->string;

        dd($uuid);
    }
}

How To Check a UUID is Valid?

Laravel Str class provides a method i.e isUuid. We 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
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');

        dd($isUuid);
    }
}

Concept

$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');

Output

true

We hope this article helped you to learn Laravel 9 How To Generate UUID in Application 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.