CluPilotCloud/app/Models/Concerns/HasUuid.php

27 lines
557 B
PHP

<?php
namespace App\Models\Concerns;
use Illuminate\Support\Str;
/**
* Assigns a random UUID on creation. Records that expose a `uuid` column use it
* as their route key (R11: URLs address records by UUID, not integer PK).
*/
trait HasUuid
{
protected static function bootHasUuid(): void
{
static::creating(function ($model) {
if (empty($model->uuid)) {
$model->uuid = (string) Str::uuid();
}
});
}
public function getRouteKeyName(): string
{
return 'uuid';
}
}