Eloquent UUID
Published on by Paul Redmond
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.