An Eloquent UUID Trait to use with Laravel > 5.6
composer require lucadello91/eloquent-uuid
For using uuid in your Eloquent Model, just put use UuidModelTrait;
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Lucadello91\EloquentUuid\UuidModelTrait;
class User extends Model
{
use UuidModelTrait;
}
This package will use UUID v4 values by default.
You can use uuid1
, uuid3
, uuid4
, uuid5
or ordered
by setting the protected property $uuidVersion
in your Eloquent Model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Lucadello91\EloquentUuid\UuidModelTrait;
class Post extends Model
{
use UuidModelTrait;
protected $uuidVersion = 'uuid5';
}
When using UuidModelTrait, you have to use uuid
your schema :
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
//...
});
If you prefer to use a binary UUID in your database, you just need to cast your primary key to uuid
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Lucadello91\EloquentUuid\UuidModelTrait;
class User extends Model
{
use UuidModelTrait;
protected $keyType = 'uuid';
//or
protected $casts = [
'id' => 'uuid',
];
}
To run the tests, just run composer install
and ./vendor/bin/phpunit
.
Please see CHANGELOG for more information what has changed recently.
The MIT License (MIT). Please see License File for more information.