Skip to content

Repository files navigation

PHP Version : 8.3 | 8.4 | 8.5 Laravel Version : 13.x License Build
Source Packagist Version Packagist Downloads

Simple, predictable slugs for Laravel Eloquent models.

Powered by PHP Slugify.

Installation

composer require pharaonic/laravel-sluggable

Usage

Laravel Sluggable can generate and maintain slugs automatically through Eloquent model events.

PHP Attributes

Configure your slug directly on the model using the Slug attribute:

use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Sluggable\Attributes\Slug;
use Pharaonic\Laravel\Sluggable\Sluggable;

#[Slug(source: 'title')]
class Post extends Model
{
    use Sluggable;
}

The default target column is slug:

#[Slug(source: 'title', target: 'slug')]

Need more than one slug? Repeat the attribute:

#[Slug(source: 'title', target: 'slug', unique: true)]
#[Slug(source: 'seo_title', target: 'seo_slug')]
class Post extends Model
{
    use Sluggable;
}

Available attribute options include:

source, target, separator, unique, on_create, on_update, include_trashed, max_length, ascii_only, and ascii_lang.

Omitted options inherit the package defaults.

Use max_length: 0 when no length limit is required.

Callable Sources

PHP Attributes are designed for simple column-based sources.

For computed values, multiple columns, relations, or custom logic, use the existing sluggable() method:

public function sluggable(): array
{
    return [
        'slug' => [
            'source' => fn ($model) => $model->category->name.' '.$model->title,
        ],
    ];
}

Configuration Priority

Laravel Sluggable keeps all configuration styles backward compatible.

When more than one style is available, the package resolves them in this order:

sluggable() method
    ↓
PHP Attributes
    ↓
$sluggable property
    ↓
Package defaults

Definitions from different styles are not merged.

Package defaults only fill missing options for the selected configuration.

Existing $sluggable properties and sluggable() methods continue to work unchanged.

Documentation

For the complete API, configuration options, inheritance rules, validation behavior, and advanced usage, see the Laravel Sluggable Documentation.

Contributing

Contributions are welcome. See CONTRIBUTING.md.

Support & Security

See SUPPORT.md and SECURITY.md.

License

MIT. See LICENSE.

Releases

Sponsor this project

Used by

Contributors

Languages