Skip to content

Instantly share code, notes, and snippets.

@osbre
Created July 7, 2025 11:50
Show Gist options
  • Save osbre/2cf63ee5d3c7956c882b2f5adb296416 to your computer and use it in GitHub Desktop.
Save osbre/2cf63ee5d3c7956c882b2f5adb296416 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\Concepts;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/** @mixin Model */
trait HasUUID
{
public static function bootHasUUID(): void
{
static::creating(function ($model) {
if ($model->{$model->getKeyName()} === null) {
$model->{$model->getKeyName()} = (string)Str::uuid();
}
});
}
public function initializeHasUUID(): void
{
$this->keyType = 'string';
$this->incrementing = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment