spatie / laravel-sluggable
Generate slugs when saving Eloquent models
Fund package maintenance!
Requires
- php: ^8.3
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.24
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- spatie/laravel-translatable: ^6.0
- dev-main
- v4.x-dev
- 4.0.0
- v3.x-dev
- 3.8.1
- 3.8.0
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.x-dev
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- v1.x-dev
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
- dev-fix/translatable-prevent-overwrite-v2
- dev-l13-support
- dev-phpunit-8
- dev-laravel-5.8
This package is auto-updated.
Last update: 2026-04-27 14:19:20 UTC
README
Generate slugs when saving Eloquent models
This package generates a unique slug for any Eloquent model whenever it is created or updated. Add a #[Sluggable] attribute to the model and the package handles 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"
For features that need closures (custom source callables, scoped uniqueness, conditional skip, custom suffix generators) use the HasSlug trait with a getSlugOptions() method instead.
Highlights
- Unique slugs out of the box, with a configurable
-1,-2, ... suffix on collisions. - Self-healing URLs: route keys that combine the slug with the primary key (
hello-world-5) so renaming a model never breaks an existing link. Stale slugs return a308redirect to the canonical URL. - Translatable slugs through
HasTranslatableSlugandspatie/laravel-translatable. - Overridable actions: swap the slug generator or the self-healing URL logic for your own class via a config file.
- Laravel Boost skill bundled with the package, so AI assistants know how to scaffold sluggable models in your project. Boost discovers it automatically once both packages are installed.
Self-healing URLs combine the slug with the primary key. The HasSlug trait is required alongside the attribute, because it overrides Eloquent's route key and route binding methods.
#[Sluggable(from: 'title', to: 'slug', selfHealing: true)] class Post extends Model { use HasSlug; } // /posts/hello-world-5 → 200 // /posts/old-title-5 → 308 to /posts/hello-world-5
Spatie is a web design agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Documentation
All documentation is available on our documentation site.
Installation
composer require spatie/laravel-sluggable
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.