lucadello91 / eloquent-uuid
Laravel Eloquent Model trait for using UUID on primary key
Installs: 8 068
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/database: ^8|^9|^10
- illuminate/support: ^8|^9|^10
- ramsey/uuid: ^3|^4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- illuminate/events: ^8|^9|^10
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.1|^10.0
This package is auto-updated.
Last update: 2024-11-18 17:37:27 UTC
README
Eloquent UUID
An Eloquent UUID Trait to use with Laravel > 5.6
Installation
composer require lucadello91/eloquent-uuid
In your models
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'; }
Database Schema
When using UuidModelTrait, you have to use uuid
your schema :
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
//...
});
Binary Uuid
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',
];
}
Running tests
To run the tests, just run composer install
and ./vendor/bin/phpunit
.
Changelog
Please see CHANGELOG for more information what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.