diff --git a/src/core/src/Database/Eloquent/Concerns/HasUlids.php b/src/core/src/Database/Eloquent/Concerns/HasUlids.php index e820e2b4..4ed5a747 100644 --- a/src/core/src/Database/Eloquent/Concerns/HasUlids.php +++ b/src/core/src/Database/Eloquent/Concerns/HasUlids.php @@ -6,22 +6,27 @@ use Hyperf\Stringable\Str; +/** + * Provides ULID primary key support for Eloquent models. + */ trait HasUlids { /** - * Add a unique identifier to the model before it is created. + * Boot the trait. */ - public function creating(): void + public static function bootHasUlids(): void { - foreach ($this->uniqueIds() as $column) { - if (empty($this->{$column})) { - $this->{$column} = $this->newUniqueId(); + static::registerCallback('creating', function (self $model): void { + foreach ($model->uniqueIds() as $column) { + if (empty($model->{$column})) { + $model->{$column} = $model->newUniqueId(); + } } - } + }); } /** - * Generate a new UUID for the model. + * Generate a new ULID for the model. */ public function newUniqueId(): string { diff --git a/src/core/src/Database/Eloquent/Concerns/HasUuids.php b/src/core/src/Database/Eloquent/Concerns/HasUuids.php index 30eaa98e..f5add066 100644 --- a/src/core/src/Database/Eloquent/Concerns/HasUuids.php +++ b/src/core/src/Database/Eloquent/Concerns/HasUuids.php @@ -6,18 +6,23 @@ use Hyperf\Stringable\Str; +/** + * Provides UUID primary key support for Eloquent models. + */ trait HasUuids { /** - * Add a unique identifier to the model before it is created. + * Boot the trait. */ - public function creating(): void + public static function bootHasUuids(): void { - foreach ($this->uniqueIds() as $column) { - if (empty($this->{$column})) { - $this->{$column} = $this->newUniqueId(); + static::registerCallback('creating', function (self $model): void { + foreach ($model->uniqueIds() as $column) { + if (empty($model->{$column})) { + $model->{$column} = $model->newUniqueId(); + } } - } + }); } /**