How to generate UUID in Laravel Example

Laravel UUID generator example; In this tutorial you will learn how to generate uuid in laravel application. UUID stands for Universal Unique Identifier. It’s a 128-bit number used to uniquely identify some object or in our case, a record in our database.

Generating uuid in laravel nothing do anything just use Str class. You can use the uuid as a primary key of your database table.

Generate UUID in Laravel using Str Facade

For generating UUId in laravel you just need to use the use Illuminate\Support\Str; facdes and then you can generate using Str::uuid(), see the below example;

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

class PostController extends Controller
{
    public function index()
    {
        $uuid = (string) Str::uuid();

        dd($uuid);
    }
}

I hope you enjoy with the example of how to generate uuid in laravel applicaiton.

1 thought on “How to generate UUID in Laravel Example”

Leave a Comment