LaDo/app/Traits/UuidTrait.php

42 lines
766 B
PHP

<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait UuidTrait
{
/**
* Boot function from Laravel.
*/
protected static function boot(): void
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing(): bool
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType(): string
{
return 'string';
}
}