Current Location: Home> Latest Articles> Complete Guide to Auto-Generating UUIDs in Laravel: From Database Migration to Implementation

Complete Guide to Auto-Generating UUIDs in Laravel: From Database Migration to Implementation

gitbox 2025-06-16

1. Introduction

Laravel is a popular PHP framework designed to offer clean, maintainable, and highly expressive code. It comes with many powerful and practical features, one of which is the ability to automatically generate UUIDs. UUIDs (Universal Unique Identifiers) are widely used in various systems, especially in scenarios where unique identification of information is required.

2. What is UUID

UUID, which stands for Universally Unique Identifier, is a 128-bit value used to uniquely identify information. The generation of UUIDs does not depend on a central coordinator, meaning they can be generated independently across different computers or systems without the risk of duplication. This makes UUIDs a crucial tool in distributed systems and is widely used in databases, network protocols, and more.

3. How Laravel Auto-generates UUIDs

3.1 Creating a Table with a UUID Field

To use UUIDs in Laravel, you first need to add a UUID field in your database migration file. You can set it up as follows:


Schema::create('my_table', function (Blueprint $table) {
    $table->uuid('uuid')->unique();
    // other fields...
});

In the code above, we define a field called `uuid` with the type UUID and set it to be unique using the `unique()` method to ensure that each UUID value is unique.

3.2 Laravel Auto-generating UUIDs

Once you've added the UUID field in your table, Laravel will automatically generate a UUID for each new record. In your model, you can implement this logic as follows:


use Ramsey\Uuid\Uuid;

class MyModel extends Model
{
    public $incrementing = false;
    protected $primaryKey = 'uuid';

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->uuid = Uuid::uuid4()->toString();
        });
    }
}

In the code above, we use the `Ramsey\Uuid\Uuid` class and generate a UUID inside the model's `creating` event. This ensures that before saving the model, a UUID is assigned to the `uuid` field.

4. How to Use the Auto-generated UUID

Once you've set up the UUID generation in your model, you can use the model as you would normally in Laravel. For instance, when creating a new record, the UUID will be automatically generated and inserted into the corresponding field in the database:


App\Models\MyModel::create([
    'name' => 'my name',
    // other fields...
]);

When this insert operation is executed, Laravel will automatically generate a unique UUID and save it in the `uuid` field.

5. Conclusion

With Laravel's auto-generating UUID feature, developers can easily create unique identifiers for each record. By adding a UUID field in the database migration and implementing UUID generation in the model, the process is automated. Leveraging UUIDs' distributed nature ensures that unique identifiers are generated across multiple systems, avoiding data conflicts and enhancing consistency.