cloudcake/laravel-uuid is a Laravel package for uuid's for laravel's eloquent models..
It currently has 1 GitHub stars and 131 downloads on Packagist (latest version v1.5).
Install it with composer require cloudcake/laravel-uuid.
Discover more Laravel packages by cloudcake
or browse all Laravel packages to compare alternatives.
Last updated
Since Laravel 9.x, UUID support for Eloquent is baked right into Laravel.
See Illuminate/Database/Eloquent/Concerns/HasUuids.
This package is now redundant.
Install via composer using composer require cloudcake/laravel-uuid
If you want to use UUID's as your model's primary key, this is for you.
Uuid\Traits\UuidPrimaryKey trait to the eloquent modelid (Optional)use Uuid\Traits\UuidPrimaryKey;
class User extends Authenticatable
{
use UuidPrimaryKey;
protected $primaryKey = 'id';
}
Now you'll be able to call User::find('<uuid-here>');.
In some situations you may want to retain your regular integer based primary key, but add an additional UUID column to your models, for this case, use the Uuid trait.
Uuid\Traits\Uuid trait to the eloquent modeluse Uuid\Traits\Uuid;
class User extends Authenticatable
{
use Uuid;
protected static $uuidKeyName = 'uuid';
}
And query it as you would any other attribute, User::where('uuid', '<uuid-here>')->first().
Add any/all of the following to your model(s) to fine-tune the package to your needs.
| Attribute | Description |
-------------- | -------------------------------------------------------------------------------------|
| $uuidKey | The name of the UUID column in the database. Defaults to uuid. |
| $uuidOrdered | Whether or not the UUID should use timestamp ordering. Defaults to true. |
Example:
use Uuid\Traits\Uuid;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use Uuid;
/**
* The name of the UUID column in the database.
*
* @var string
*/
protected static $uuidKey = 'universally_unique_id';
/**
* Whether or not the UUID should use timestamp ordering.
*
* @var bool
*/
protected static $uuidOrdered = true;
}