Skip to content

Configuration

Via #[Slugify] Attribute

The #[Slugify] attribute accepts the following parameters:

ParameterTypeDefaultDescription
fromstring|array(required)Source attribute(s) or method name
tostring|nullgetRouteKeyName()Column to save the slug to
separatorstring|null'-'Word separator character
maxLengthint|nullnullMaximum slug length (truncates at word boundaries)
regenerateOnUpdatebooltrueWhether to regenerate on source change

Examples

php
// Full configuration
#[Slugify(from: 'name', to: 'slug')]
class Post extends Model
{
    use HasSlug;
}

// Only 'from' — slug column determined by getRouteKeyName()
#[Slugify(from: 'name')]
class Post extends Model
{
    use HasSlug;

    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}

// Multiple source attributes
#[Slugify(from: ['first_name', 'last_name'], to: 'slug')]
class Author extends Model
{
    use HasSlug;
}
// first_name: "John", last_name: "Doe" → "john-doe"

// Custom separator
#[Slugify(from: 'title', to: 'slug', separator: '_')]
class Post extends Model
{
    use HasSlug;
}
// "Hello World" → "hello_world"

// Method source for complex slug generation
#[Slugify(from: 'getFullTitle', to: 'slug')]
class Post extends Model
{
    use HasSlug;

    public function getFullTitle(): string
    {
        return $this->category->name . ' ' . $this->title;
    }
}
// "Tech Laravel Tips" → "tech-laravel-tips"

// SEO-safe — slug only generated on creation
#[Slugify(from: 'title', to: 'slug', regenerateOnUpdate: false)]
class Post extends Model
{
    use HasSlug;
}

WARNING

The to parameter only controls where the slug is saved. For route model binding, you still need to override getRouteKeyName() separately on your model.

Via Method Overrides

Alternatively, configure slug generation by overriding methods:

MethodReturnsDescription
getAttributeToCreateSlugFrom()string|arraySource attribute(s)
getRouteKeyName()stringSlug column for route binding
getAttributeToSaveSlugTo()stringColumn to save slug (if different from route key)
getSlugSeparator()stringSeparator character (default '-')
getMaxSlugLength()?intMax length, truncated at word boundaries
shouldRegenerateSlugOnUpdate()boolWhether to regenerate on update (default true)
getSlugLanguage()stringLanguage for transliteration (default 'en')
scopeSlugQuery()BuilderScope for uniqueness checks
php
use Oliwol\Slugify\HasSlug;

class Post extends Model
{
    use HasSlug;

    public function getAttributeToCreateSlugFrom(): string|array
    {
        return 'name';
    }

    public function getRouteKeyName(): string
    {
        return 'slug';
    }

    public function getAttributeToSaveSlugTo(): string
    {
        return 'slug';
    }

    public function scopeSlugQuery($query)
    {
        return $query->where('tenant_id', $this->tenant_id);
    }
}

Released under the MIT License.