Блог Серії
Кар'єра
Вакансії Компанії
Навчання
Співбесіди Тестування Відео
Екосистема
Пакети Ресурси Проєкти
Інше
Події Про нас

Laravel Sluggable

spatie/laravel-sluggable
4.0.2 13 1,547 12.9M 30 квітня 2026 4
На GitHub

Автоматично генерує slug-и при збереженні Eloquent моделей.

Поділитись

README

Logo for laravel-sluggable

Generate slugs when saving Eloquent models

Latest Version on Packagist MIT Licensed GitHub Workflow Status Total Downloads

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 a 308 redirect to the canonical URL.
  • Translatable slugs through HasTranslatableSlug and spatie/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.

Коментарі

Увійдіть, щоб залишити коментар

Будьте першим, хто залишить коментар!

Схожі пакети

Laravel Query Builder

spatie/laravel-query-builder

Легко будуйте Eloquent-запити на основі запитів від API.

4,455 7.3.0 13 6

Eloquent Sluggable

cviebrock/eloquent-sluggable

Легке створення слагів для ваших моделей Eloquent у Laravel.

3,993 13.0.0 13 36

Laravel Auditing

owen-it/laravel-auditing

Пакет для аудиту змін Eloquent моделей у Laravel, що дозволяє автоматично відстежувати та записувати всі модифікації даних.

3,440 v14.0.6 13 4

Sushi

calebporzio/sushi

Додатковий драйвер для Eloquent, який дозволяє працювати з масивами даних як з повноцінними Eloquent моделями.

3,005 v2.5.4 13 8

Iseed

orangehill/iseed

Генерує новий файл seed для Laravel на основі даних з існуючої таблиці бази даних.

2,896 v3.8.1 13 4

Eloquent Has Many Deep

staudenmeir/eloquent-has-many-deep

Laravel Eloquent відносини HasManyThrough з необмеженою кількістю рівнів вкладеності.

2,861 v1.22.1 4