27 lines
565 B
PHP
27 lines
565 B
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
trait HasUuid
|
|
{
|
|
protected static function bootHasUuid(): void
|
|
{
|
|
static::creating(function ($model) {
|
|
foreach ($model->getUuidColumns() as $column) {
|
|
if (empty($model->{$column})) {
|
|
$model->{$column} = (string) Str::uuid();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public function getUuidColumns(): array
|
|
{
|
|
return property_exists($this, 'uuidColumns')
|
|
? $this->uuidColumns
|
|
: ['uuid'];
|
|
}
|
|
}
|