- How do I install and set up spatie/laravel-sluggable in a Laravel project?
- Run `composer require spatie/laravel-sluggable` and add the `#[Sluggable]` attribute to your Eloquent model. For example, `#[Sluggable(from: 'title', to: 'slug')]` on a `Post` model will auto-generate slugs from the `title` field. No migrations are required if you already have a `slug` column.
- Does this package work with Laravel 10+ and PHP 8.1+?
- Yes, the package is fully compatible with Laravel 10+ and requires PHP 8.1+. It leverages PHP 8’s attributes (e.g., `#[Sluggable]`) for cleaner syntax, but also supports the older `HasSlug` trait for advanced use cases.
- How does the self-healing URL feature prevent broken links when slugs change?
- Self-healing URLs combine the slug with the model’s ID (e.g., `post/hello-world-5`). If a slug changes, old URLs (e.g., `/post/hello-world`) automatically redirect (308) to the new canonical URL (`/post/hello-world-5`). This requires updating routes to include the ID.
- Can I customize slug generation for specific models (e.g., dynamic sources, conditional logic)?
- Yes, use the `HasSlug` trait and define a `getSlugOptions()` method in your model. This allows closures for custom sources, scoped uniqueness (e.g., by tenant), or conditional slug generation. Example: `return ['source' => fn() => strtolower($this->title)]`.
- What happens if two models generate the same slug? How are collisions handled?
- By default, the package appends a suffix (e.g., `-2`, `-3`) to duplicate slugs. You can customize this behavior via the `suffix` option in `getSlugOptions()` or by providing a custom suffix generator.
- Is there a performance impact for high-write applications (e.g., 10K+ records/hour)?
- Slug uniqueness checks add a query per save. For high-write workloads, consider disabling `unique: true` if duplicates are acceptable, caching slug generation results, or scoping uniqueness checks (e.g., `extraScope()` for tenant-isolated systems).
- How do I migrate from manual slug generation to this package without breaking existing URLs?
- Phase your migration: 1) Add the `slug` column if missing, 2) Update routes to include IDs (e.g., `{slug}-{id}`), and 3) implement middleware to redirect old URLs (e.g., `/old-slug` → `/old-slug-123`) using a 308 permanent redirect. Use dual routes during transition.
- Does this package support multilingual or translatable slugs?
- Yes, for translatable slugs, combine this package with `spatie/laravel-translatable` and use the `HasTranslatableSlug` trait. This generates locale-specific slugs (e.g., `es/mi-publicacion`, `fr/mon-article`) while maintaining uniqueness per language.
- Can I use this package with non-ASCII characters (e.g., Cyrillic, CJK) in slugs?
- Yes, the package handles Unicode characters via the `language` option (e.g., `language: 'ru'` for Russian). Test with your app’s locales to ensure transliteration works as expected. Default behavior uses Laravel’s `Str::slug()` under the hood.
- Are there alternatives to this package for Laravel slug generation?
- Alternatives include `cviebrock/eloquent-sluggable` (older, trait-based) and `archtechx/trait-sluggable` (lightweight). However, `spatie/laravel-sluggable` stands out for its self-healing URLs, PHP 8+ attribute support, and seamless integration with Laravel’s routing and testing ecosystem.