Eloquent UUID

Eloquent UUID

Eloquent UUID is a package by Matt Inamdar that is a simple drop-in solution for providing UUID (version 4) support for the IDs of your Eloquent models.

To use this package, your model needs to extend from the model class provided by this package:

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;

class BlogPost extends Model
{
    //
}

Note: the package also provides a model that satisfies the Authenticatable interface for the User model.

When you create a model, a UUID is automatically generated as the primary key:

// No UUID provided (automatically generated).
$model = Model::create();
echo $model->id; // abb034ae-fcdc-4200-8094-582b60a4281f

You can also specify your own UUID if you need to know the ID of the model before it’s created:

// UUID explicity provided.
$model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e

You can learn more about this package, get full installation instructions, and view the source code on GitHub at goldspecdigital/laravel-eloquent-uuid.

Filed in: News

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Full-Stack Engineer
[REMOTE: USA Only] All Other Applications Will be Rejected Immediately"
Curricula
Senior Full Stack PHP Developer
Canada
Motto Design Studio
👉 Looking for Senior Back-End Laravel API Developer - Immediate (Full-time Contractor) 🎉
Remote
ApproveMe.com // The Document Signing Experience™
Senior Software Engineer
Remote or Los Angeles
Coverfly
Sr. Software Engineer
Salt Lake City, UT
Clearlink