How to Generate UUID in Laravel?

How to Generate UUID in Laravel

In today’s web applications, many developers choose to use UUIDs (Universally Unique Identifiers) rather than using an increasing integer ID. This has proven to provide better security and unique identification for different systems, as well as scalability, especially when working with distributed applications or APIs.

Laravel has made it easy and beautiful for developers to work with UUIDs. In this article, I will outline how to generate UUIDs in Laravel.

Laravel Development Services

Prerequisite

1. Composer (latest Version)

2. Laravel version 12

Steps to Generate UUID in Laravel:

1. Create a new Laravel project

2. Configure the database

3. Create a model and migration

4. Update migration to use UUID

5. Automatically generate UUID in model

6. Create Controller to Test UUID

7. Add Route

8. Test in Browser

Step 1: Create a New Laravel Project

composer create-project laravel/laravel uuid-demo
got to inside,
cd uuid-demo

Step 2: Configure Database

Open the .env file and update the database details as per the need.

Step 3: Create Model and Migration

Now, create the model

php artisan make:model Post -m

Step 4: Update Migration to Use UUID

Open the migration file and update the column for uuid,

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('title');
        $table->text('content');
        $table->timestamps();
    });
}

After editing this, run the command

php artisan migrate

Step 5: Automatically Generate UUID in Model

Open the post model and update it,

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
class Post extends Model
{
    use HasUuids;
    protected $fillable = ['title', 'content'];
    public $incrementing = false;
    protected $keyType = 'string';
  public function uniqueIds()
    {
        return ['id'];
    }
}

Step 6: Create Controller to Test UUID

php artisan make:controller PostController

Now, open this controller and edit it,

use App\Models\Post;
public function store()
{
    $post = Post::create([
        'title' => 'UUID Demo Blog Post',
        'content' => 'This post is created using UUID String Function.'
    ]);
    return $post;
}

Step 7: Add Route

use App\Http\Controllers\PostController;
Route::get('/create-post', [PostController::class, 'store']);

Step 8: Test in Browser

http://127.0.0.1:8000/create-post

Output: 

Now, go to databse and you will see this type of columns with unique id as below.

Conclusion:

UUIDs provide an interesting option as compared to traditional numeric IDs for Laravel applications. The combination of Laravel’s built-in helpers makes it very easy to generate and work with UUIDs, which contain several advantages over using numeric IDs. They also create a level of security and scalability that numeric IDs do not provide for your application while developing APIs, SaaS, or distributed systems.

Hire laravel Developer

FAQ

1. What version of UUID does Laravel use?

Laravel uses UUID v4 UUIDs, which are generated using the Str::uuid() function. These UUIDs are random and offer good security.

2. Are UUIDs slower than auto-increment IDs?

The size of the UUID is larger, and indexing may be slower than an Auto-Increment ID, but in most cases, the difference is negligible.

3. Can I use UUID as a primary key in Laravel?

Yes, you can use UUIDs as primary keys in Laravel, provided that your model is configured to use UUIDs as your primary key.

Previous Article

Top Shopify Bundle Apps for Higher Conversions

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨