33 lines
667 B
PHP
33 lines
667 B
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
trait HasPublicIdentifier
|
|
{
|
|
public static function bootHasPublicIdentifier(): void
|
|
{
|
|
static::creating(function ($model) {
|
|
if (empty($model->ulid)) {
|
|
$model->ulid = (string) Str::ulid();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'ulid';
|
|
}
|
|
|
|
public static function findByUlid(string $ulid): ?static
|
|
{
|
|
return static::where('ulid', $ulid)->first();
|
|
}
|
|
|
|
public static function resolveUlidToId(string $ulid): ?int
|
|
{
|
|
return static::where('ulid', $ulid)->value('id');
|
|
}
|
|
}
|