ignition-nbs / laravel-uuid-model
Enable Laravel's Eloquent Model to be identified by UUID (string) rather than ID (increment or bigIncrement)
Installs: 1 376
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- ramsey/uuid: ^4.0
This package is auto-updated.
Last update: 2025-03-24 06:30:21 UTC
README
laravel-uuid-model provides 1 PHP Trait that implements the initialization
function and is thus called by the Illuminate\Database\Eloquent\Model
constructor. The initializeUuidModel
sets $incrementing
to FALSE, $keyType
to"string"
, and it will ensure that the id
attribute can be mass-assigned.
Then it generates a version 4 universally unique identifier for the id
attribute and sets that value already in $this->attributes['id']
.
It is important to note that this Trait works better then UUID Model classes
that extend Laravel's Model
class: Classes that have intermediate parent
classes (e.g. App\User
) will not work with those type of UUID Model classes.
This package works with Laravel 5.7, 5.8, 6.x and 7.x.
Installation
$ composer require ignition-nbs/laravel-uuid-model
If you do install and use this package, please send a postcard to
Ignition NBS Ltd
5th Floor
82 King Street
Manchester
M2 4WQ
United Kingdom
Usage
There are two things that you must do in order to make successful use of this
UuidModel
:
- Use
IgnitionNbs\LaravelUuidModel\UuidModel
inside your model class:
<?php namespace App; use IgnitionNbs\LaravelUuidModel\UuidModel; use Illuminate\Database\Eloquent\Model; class MyModel extends Model { use UuidModel; }
- Set the
id
field in the corresponding migration by using theuuid()
function:
class CreateMyModelsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('my_models', function (Blueprint $table) { $table->uuid('id')->primary(); }); } }
##Changelog Please see CHANGELOG for information on what has changed recently.
##Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
##Security If you discover any security related issues, please email guusleeuw@ignitionnbs.co.uk instead of using the issue tracker.
##Credits
##License MIT