*/ protected $fillable = [ 'name', 'slug', 'description', ]; /** * Boot the model and register model events. */ protected static function booted(): void { static::creating(function (Category $category): void { if (blank($category->slug)) { $category->slug = static::generateUniqueSlug($category->name); } }); static::updating(function (Category $category): void { if ($category->isDirty('name') && blank($category->slug)) { $category->slug = static::generateUniqueSlug($category->name, $category->id); } }); } /** * Get the posts associated with the category. */ public function posts(): HasMany { return $this->hasMany(Post::class); } /** * Generate a unique slug based on the category name. */ public static function generateUniqueSlug(string $name, ?int $ignoreId = null): string { $baseSlug = Str::slug($name); $slug = $baseSlug; $counter = 1; while (static::query() ->where('slug', $slug) ->when($ignoreId, fn ($query) => $query->whereKeyNot($ignoreId)) ->exists()) { $slug = "{$baseSlug}-{$counter}"; $counter++; } return $slug; } }