DEV Community

Muhamad Jamil
Muhamad Jamil

Posted on

generate unique slug with eloquent-sluggable package laravel 8

First install the package with composer composer require cviebrock/eloquent-sluggable

and simply put this code in your model

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

it will automatically use title as a slug

for more feature, maybe you want to read the documentations

thanks for reading, if you have a better method, feel free to discuss

Top comments (0)