Generate slugs when saving Eloquent models
spatie/laravel-sluggable is a Laravel package for generate slugs when saving eloquent models.
It currently has 1.545 GitHub stars and 12.746.959 downloads on Packagist (latest version 4.0.2).
Install it with composer require spatie/laravel-sluggable.
Discover more Laravel packages by spatie
or browse all Laravel packages to compare alternatives.
Last updated
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.
-1, -2, ... suffix on collisions.hello-world-5) so renaming a model never breaks an existing link. Stale slugs return a 308 redirect to the canonical URL.HasTranslatableSlug and spatie/laravel-translatable.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.
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.
All documentation is available on our documentation site.
composer require spatie/laravel-sluggable
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.