Created
July 7, 2025 11:50
-
-
Save osbre/2cf63ee5d3c7956c882b2f5adb296416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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