Laravel Sluggable
An opinionated Laravel package that generates unique, self-healing slugs for Eloquent models on save.
Repository Health
Technical Analysis
Laravel Sluggable is a Spatie package that automatically generates a URL-friendly slug for any Eloquent model whenever it is created or updated. You add a #[Sluggable] attribute (or the HasSlug trait with a getSlugOptions() method) and the package derives the slug from one or more source fields, guaranteeing uniqueness with an incrementing suffix on collisions.
Beyond basic slugs it offers self-healing URLs that combine the slug with the model’s primary key so renaming never breaks a link, scoped uniqueness, conditional generation, translatable slugs via spatie/laravel-translatable, and overridable action classes for full customization.
What You Get
- Automatic slug generation on model create and update from one or more source fields.
- Guaranteed uniqueness with configurable
-1,-2collision suffixes and scoped uniqueness. - Self-healing URLs that append the primary key and 308-redirect stale slugs to the canonical URL.
- Translatable slugs, conditional generation and overridable slug-generator action classes.
Common Use Cases
- Generating clean, SEO-friendly URLs for blog posts, products or pages.
- Keeping links stable when a record’s title changes via self-healing route keys.
- Producing localized slugs per locale in a multilingual application.
Under The Hood
Architecture
The package is built around the HasSlug trait and a #[Sluggable] attribute that register an Eloquent saving observer; SlugOptions captures configuration, a slug-generator action slugifies source fields and resolves collisions, and optional self-healing route-key overrides combine the slug with the primary key for redirect handling.
Tech Stack
PHP targeting current Laravel versions, distributed via Composer; integrates with Eloquent, optionally spatie/laravel-translatable for translatable slugs, and ships PHPStan config and a Pest/PHPUnit test suite plus a bundled Laravel Boost skill.
Code Quality
Typical of Spatie’s libraries: small, focused, fully typed and thoroughly tested under tests/, with static analysis via PHPStan and clear separation between the trait, options object and overridable action classes; documentation and an UPGRADING guide are maintained.
API Design
The developer experience is excellent - a single attribute covers the common case while the HasSlug trait plus getSlugOptions() unlocks closures, scoped uniqueness and conditional generation, and every behavior is overridable through published config, keeping the learning curve low.