mindtwo/laravel-auto-create-uuid
Auto-fill a UUID v4 on Eloquent models when creating or replicating. Add a trait, add a uuid column, and it just works—no config. Supports custom UUID column names and ensures replicas get a fresh UUID by excluding the UUID attribute on replicate.
Automatically populate a UUID column on Eloquent models when they are created or replicated. Drop the trait onto any model and the rest works without configuration.
Install the package via Composer:
composer require mindtwo/laravel-auto-create-uuid
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;
class Example extends Model
{
use AutoCreateUuid;
}
$table->uuid('uuid')->unique();
The trait listens to the creating and replicating model events and fills
the configured UUID column with a new UUID v4 whenever the column is empty or
holds an invalid UUID.
The default column name is uuid. To use a different column, either define a
$uuid_column property:
class Example extends Model
{
use AutoCreateUuid;
protected string $uuid_column = 'identifier';
}
or override the getUuidColumn() method:
class Example extends Model
{
use AutoCreateUuid;
public function getUuidColumn(): string
{
return 'identifier';
}
}
The trait overrides Model::replicate() to ensure the UUID column is excluded
from the cloned attributes, so the replica receives a fresh UUID via the
replicating event.
composer test
composer analyse
composer format
Please see CHANGELOG for a list of recent changes.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email info@mindtwo.de instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?