- How do I quickly add slug generation to a Laravel Eloquent model?
- Use the #[Sluggable] attribute on your model class. For example, `#[Sluggable(from: 'title', to: 'slug')]` will auto-generate a slug from the `title` field and store it in the `slug` column. No additional configuration is needed for basic use.
- What Laravel versions and PHP versions does spatie/laravel-sluggable support?
- The package supports Laravel 9+ and PHP 8.0+. It leverages modern Laravel features like attributes and traits, so ensure your project meets these requirements for full compatibility.
- How does the self-healing URL feature work, and should I enable it?
- Self-healing URLs combine the slug with the model’s ID (e.g., `hello-world-5`). If the slug changes, old URLs automatically redirect (308) to the new canonical URL, preventing 404s. Enable it globally or per-model via the `HasSlug` trait, but note it requires updating route bindings to use `{model}` instead of `{model:slug}`.
- Can I customize how slugs are generated (e.g., custom separators, special characters)?
- Yes. Use the `HasSlug` trait and define a `getSlugOptions()` method to override defaults. For example, you can change the separator from `-` to `--` or use a custom slug generator. The package also supports closures for dynamic slug logic.
- How do I handle slug collisions (duplicate slugs) in my application?
- The package automatically appends a suffix (e.g., `-1`, `-2`) to duplicate slugs. You can customize this behavior via `getSlugOptions()` or configure the suffix generator in the package’s config file.
- Does this package work with multilingual content (e.g., spatie/laravel-translatable)?
- Yes. Use the `HasTranslatableSlug` trait alongside `spatie/laravel-translatable` to generate locale-specific slugs. The package integrates seamlessly, allowing you to define translatable slug sources and scopes.
- What happens if I retroactively enable self-healing URLs on an existing Laravel project with live traffic?
- Retrofitting self-healing URLs requires updating route bindings and implementing 301 redirects from old slug-based URLs (e.g., `/posts/old-slug`) to new self-healing URLs (e.g., `/posts/new-slug-123`). Test thoroughly in staging and use feature flags for a gradual rollout.
- How do I test slug generation and self-healing redirects in my Laravel tests?
- Mock the `getRouteKey()` method in your tests to simulate stale slugs. Use Laravel’s HTTP tests to verify redirects (e.g., `assertRedirect()`) and check that the slug is generated correctly by inspecting the saved model or database.
- Are there performance concerns with uniqueness checks on every model save?
- Uniqueness checks add a database query per save, which is negligible for most applications. For high-write workloads, consider caching slugs (e.g., Redis) or adding database-level uniqueness constraints (e.g., `UNIQUE(slug, tenant_id)`) to reduce query load.
- What are some alternatives to spatie/laravel-sluggable for Laravel slug generation?
- Alternatives include `cviebrock/eloquent-sluggable`, which offers similar functionality but with a different API (e.g., `sluggable()` method). For simpler needs, you could manually generate slugs using `Str::slug()`, but this lacks collision handling and self-healing. Spatie’s package stands out for its opinionated yet flexible approach and Laravel Boost integration.