PHP & Laravel

Eloquent Sluggable

Eases using and generating slugs Laravel Eloquent models.
GitHub RepositoryReleasesPackagist

Requirements

  • PHP ^8.3
  • Laravel ^13.0

Installation

composer require kduma/eloquent-sluggable

Setup

Add the Sluggable trait to your model and create a slug column in your migration:

use KDuma\Eloquent\Sluggable;

class Post extends Model
{
    use Sluggable;
}
$table->string('slug')->unique();

Configuration

use KDuma\Eloquent\Sluggable;
use KDuma\Eloquent\Attributes\HasSlug;

#[HasSlug(from: 'name', field: 'slug')]
class Product extends Model
{
    use Sluggable;
}

Old style — model properties (deprecated)

class Post extends Model
{
    use Sluggable;

    protected string $sluggable_from = 'name';  // ⚠️ deprecated
    protected string $slug_field = 'slug';       // ⚠️ deprecated
}

Default behaviour

Without any configuration, reads from title and stores in slug.

Usage

  • Slug is auto-generated on create and regenerated on update if empty
  • $model->generateSlug() — manually regenerate (save afterwards)
  • Model::whereSlug($slug) — query scope
  • $model->getSlugField() — returns configured slug field name

Override SluggableString() for a custom slug source:

protected function SluggableString(): string
{
    return $this->year . ' ' . $this->title;
}
Copyright © 2026