You can install the package via composer:
composer require mindtwo/laravel-auto-create-uuid
Simply use the AutoCreateUuid trait in your eloquent model.
namespace example;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;
class Example extends Model
{
use AutoCreateUuid;
}
Make sure to add the column in your migration file.
$table->string('uuid', 36)->unique();
The default attribute name for the auto generated UUID is 'uuid'. However you can customize it, if you need. There are two possibilities to do so.
Either you set up a property named 'uuid_column':
namespace example;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;
class Example extends Model
{
use AutoCreateUuid;
protected $uuid_column = 'id'
}
or you overload the getUuidColumn() method:
namespace example;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;
class Example extends Model
{
use AutoCreateUuid;
/**
* Get the column name for uuid attribute.
*
* @return string
*/
public function getUuidColumn(): string
{
return 'id';
}
}
In both cases the attribute name for the UUID is now 'id' instead of 'uuid'.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.