43 lines
832 B
PHP
43 lines
832 B
PHP
<?php
|
|
|
|
namespace App\Traits\Models;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
trait HasUuidKey
|
|
{
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
parent::booted();
|
|
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 boolean
|
|
*/
|
|
public function getIncrementing(): bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the auto-incrementing key type.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getKeyType(): string
|
|
{
|
|
return 'string';
|
|
}
|
|
}
|