dudic/laravel-slug-generator

1.0.1 2022-12-01 16:11 UTC

This package is auto-updated.

Last update: 2025-06-29 02:16:40 UTC


README

Latest Version on Packagist MIT Licensed Tests Total Downloads

This package was created to generate slug on my personal projects, it is straightforward to use, you just need to install it, add trait to the model, and set the $slugConfiguration property in your model.

NOTE: Tested for 9.* and 8.* version of Laravel.

Installation

You can install the package via composer:

composer require dudic/laravel-slug-generator

Usage

use Dudic\Sluggable\SlugGenerator;

class User extends Authenticatable
{
    use SlugGenerator; // add

    protected $slugConfiguration = [
        'create_from' => ['first_name', 'last_name'], // ['required', 'array']
        'slug_field' => 'slug', // ['required', 'string']
        'unique' => true, // ['optional', 'bool'] default true, if slug exist unique will add at end '-1' or any next number which will make unique slug
        'on_create' => true, // ['optional', 'bool'] default true
        'on_update' => true, // ['optional', 'bool'] default true
        'separator' => '-', // ['optional', 'string'] default "-"
        'max_length' => 250, // ['optional', 'int', 'min:1'] default 250, if not unique can exceed 250 
    ];
}

$user = User::create([
    'first_name' => 'John',
    'last_name' => 'Doe'
]);
echo $user->slug; // john-doe

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.