mindtwo/laravel-auto-create-uuid
Laravel Auto Create UUID
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.
Installation
Install the package via Composer:
composer require mindtwo/laravel-auto-create-uuid
Requirements
- PHP 8.2+
- Laravel 10, 11, 12, or 13
Usage
Add the trait to a model
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;
class Example extends Model
{
use AutoCreateUuid;
}
Add a UUID column to the migration
$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.
Customize the UUID column
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';
}
}
Replicating models
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.
Testing
composer test
Static analysis
composer analyse
Code style
composer format
Changelog
Please see CHANGELOG for a list of recent changes.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@mindtwo.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Related Packages
Create non-incrementing Laravel models whose primary key is a UUID
This package allows you to easily work with UUIDs in your Laravel models.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 3.0 | ^8.2 | ^10.0 | | MIT | |
| 2.6.5 | ^8.0| | ^7.4| | MIT | |
| 2.6.4 | ^8.0| | ^7.4| | MIT | |
| 2.6.3 | ^8.0| | ^7.4| | MIT | |
| 2.6.2 | ^7.0| | ^7.4| | MIT | |
| 2.6.1 | ^7.0| | ^7.4| | MIT | |
| 2.6 | ^7.0| | ^7.4| | MIT | |
| 2.5 | ^7.0| | ^7.4| | MIT | |
| 2.4 | ^7.0| | ^7.4| | MIT | |
| 2.3.1 | ^7.0| | 6.*| | MIT | |
| 2.3 | ^7.3 | 6.*| | MIT | |
| 2.2 | ^7.2 | 6.*| | MIT | |
| 2.1.0 | ^7.2 | 5.6.*| | MIT | |
| 2.0.0 | ^7.1.3 | 5.6.*| | MIT | |
| 1.0 | ^7.1.3 | 5.6.*| | MIT |