Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Sluggable

Laravel Sluggable Laravel Package

spatie/laravel-sluggable

Generate unique slugs for Eloquent models on create/update. Supports collision suffixes, translatable slugs, and customizable slug options. Includes self-healing URLs that keep old links working via slug+ID route keys with 308 redirects to the canonical URL.

View on GitHub
Deep Wiki
Context7

Generate slugs when saving Eloquent models

Frequently asked questions about Laravel Sluggable
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai