ItSolutionStuff.com

How to Generate UUID in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Today i am going to share with you how to create uuid in laravel 5 application. You can create quick uuid using generator. I will use composer package for generate unique uuid.

UUID stand for universally unique identifier and is a 128-bit number used to identify information in computer pc. In this post i will show you how to generate uuid in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

you can also use Str facade to generate uuid in laravel application. i will give you two way to generate uuid in laravel.

UUID using Str Facade

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class AjaxController extends Controller

{

/**

* Write code on Construct

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$uuid = Str::uuid();

dd($uuid);

}

/**

* Write code on Construct

*

* @return \Illuminate\Http\Response

*/

public function index2(Request $request)

{

$uuid = Str::orderedUuid();

dd($uuid);

}

}

UUID using Package

In this post we will use webpatser/laravel-uuid composer package for generate uuid in laravel 5. We have to simple install webpatser/laravel-uuid package to your laravel application and then a second you can generate uuid. So, you don't have to add aliases or provides etc, just need to install webpatser/laravel-uuid package using composer require command.

So, let's simply run bellow command to install webpatser/laravel-uuid composer package to your laravel project:

Install webpatser/laravel-uuid Package:

composer require webpatser/laravel-uuid

Now we are ready to use "Uuid" facade, so within a second you can get uuid from time, string etc. So let's run bellow route for example to generate uuid.

Generate Uuid:

Route::get('uuid-gen', function () {

dd(Uuid::generate()->string);

});

Generate Uuid Output:

480c7aa0-a40e-11e7-bb4c-4fc5af23e450

As you see above example, it is very simple to generate uuid in your laravel application. You can also get more information from here : webpatser/laravel-uuid.

I hope you it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Carbon Change Timezone Example

Read Now →

Laravel Array Length Validation Example

Read Now →

How to Check Query Execution Time in Laravel?

Read Now →

Laravel Remove All Spaces from String Example

Read Now →

How to Get Request Method in Laravel?

Read Now →

Laravel Group By with Max Value Query Example

Read Now →

Laravel CURL Request Example using Ixudra/curl

Read Now →

Laravel Login with Username or Email Example

Read Now →

Laravel Ajax Request with Validation Example

Read Now →

How to Get Country City Address from IP Address in Laravel?

Read Now →

Laravel Guzzle Http Client POST Request Example

Read Now →

Laravel Create Bootstrap Contact US Form Example

Read Now →

How to Get User Agent Value in Laravel?

Read Now →

How to Get IP Address in Laravel?

Read Now →