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.
This package generates a unique slug for any Eloquent model whenever it is created or updated. Add a #[Sluggable] attribute to the class and the package takes care of the rest.
use Spatie\Sluggable\Attributes\Sluggable;
#[Sluggable(from: 'title', to: 'slug')]
class Post extends Model
{
}
$post = Post::create(['title' => 'activerecord is awesome']);
$post->slug; // "activerecord-is-awesome"
Slugs are produced by Laravel's Str::slug helper, which lowercases and replaces spaces with a configurable separator.
Head to Generating your first slug for the migration, the model, and the route binding in one walkthrough.
On top of generation, this package also ships:
spatie/laravel-translatable to store one slug per locale.Rename a post and its URL changes with it. Every bookmark, search result, and shared link pointing at the old URL now returns a 404.
Enable selfHealing and the route key becomes {slug}-{id}. The primary key resolves the model, so the slug can change without breaking lookups. Old URLs return a 308 redirect to the current one.
Read the full story in Self-healing URLs.
How can I help you explore Laravel packages today?