| Package Data | |
|---|---|
| Maintainer Username: | alsofronie |
| Maintainer Contact: | alsofronie@gmail.com (Alex Sofronie) |
| Package Create Date: | 2015-11-16 |
| Package Last Update: | 2020-05-29 |
| Home Page: | |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-11-02 15:05:03 |
| Package Statistics | |
|---|---|
| Total Downloads: | 326,062 |
| Monthly Downloads: | 1,345 |
| Daily Downloads: | 6 |
| Total Stars: | 93 |
| Total Watchers: | 5 |
| Total Forks: | 28 |
| Total Open Issues: | 5 |
An Eloquent UUID Trait to use with Laravel 5.1 - 5.4
It should work with Laravel 5.0 also, but it's untested.
The trait overwrites the static boot method and listens to the creating
event. It generates a UUID (strips the dashes) and stores it in the primary
key attribute. Thus, you'll need a CHAR(32) primary key for your model
(see migrations below).
composer require alsofronie/eloquent-uuid:dev-master
In order to make it faster, you have the option to use one of three traits:
UuidModelTrait - the key must be CHAR(36) and contains the dashesUuid32ModelTrait - the key must be CHAR(32), the dashes are strippedUuidBinaryModelTrait - the key is BINARY(16).UuidModelTraitIn order to use this trait, your schema must be something like:
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->uuid('id'); // this will create a CHAR(36) field
// or
// $table->char('id', 36);
$table->string('username', 32);
$table->string('password', 50);
// ...
$table->primary('id');
});
Uuid32ModelTraitFor this type, just use CHAR(32) in your schema (this is identical to the first one, but with stripped dashes).
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->char('id', 32);
// ...
$table->string('username', 32);
$table->string('password', 50);
$table->primary('id');
});
UuidBinaryModelTraitThis stores the key as binary. The default Laravel Blueprint curretly
does not currently support binary fields with specified length,
and (at least in MySQL) you cannot create an index (including primary key) on a BINARY field without length.
So, the schema definition should be something like this (please double check if you're not using MySQL):
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->string('username', 32);
$table->string('password', 50);
});
DB::statement('ALTER TABLE `usersb` ADD `id` BINARY(16); ALTER TABLE `usersb` ADD PRIMARY KEY (`id`);')
?>
There are two additional notes for this particular trait.
Note 1. In order to get a string representation of your uuid, simple call
$model->id_stringand you'll get it.
Note 2. You can use
User::find($uuid)with both the binary version or the string (bin2hex) version.
To use the optimized uuid, put the following line in your models:
private static $uuidOptimization = true;
In order to use this in your models, just put use Uuid[32|Binary]ModelTrait;:
<?php
namespace App;
use Alsofronie\Uuid\Uuid[32|Binary]ModelTrait;
class User extends Eloquent
{
use Uuid[32|Binary]ModelTrait;
}
To run the tests, just run composer install and ./vendor/bin/phpunit.