Laravel Sluggable

An opinionated Laravel package that generates unique, self-healing slugs for Eloquent models on save.

Library
Composer
v4.0.3
1,556stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
70/100Good
Development Activity56
Maintenance48
Community76
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
86/100Excellent
Architecture84
Code Quality88
Innovation88
Learning Curve88

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, -2 collision 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search