Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/core/src/Database/Eloquent/Concerns/HasUlids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 11 additions & 6 deletions src/core/src/Database/Eloquent/Concerns/HasUuids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
});
}

/**
Expand Down