diff --git a/src/Concerns/HasSlug.php b/src/Concerns/HasSlug.php new file mode 100644 index 0000000..62eea4f --- /dev/null +++ b/src/Concerns/HasSlug.php @@ -0,0 +1,47 @@ +slug = Str::uuid()->toString(); + }); + + static::created(function (Model&ImplementsSlug $model) { + $model->update([ + 'slug' => Str::slug(collect($model->getSlugAttributes()) + ->map(fn (string $attribute) => $model->$attribute) + ->implode(' ')), + ]); + }); + + static::updating(function (Model&ImplementsSlug $model) { + $attributes = $model->getSlugAttributes(); + if ($model->isDirty($attributes)) { + $model->slug = Str::slug(collect($model->getSlugAttributes()) + ->map(fn (string $attribute) => $model->$attribute) + ->implode(' ')); + } + }); + } +} diff --git a/src/Contracts/ImplementsSlug.php b/src/Contracts/ImplementsSlug.php new file mode 100644 index 0000000..433c7ad --- /dev/null +++ b/src/Contracts/ImplementsSlug.php @@ -0,0 +1,13 @@ +