Slugging is the process of creating a simplified, URL-friendly version of a string by converting it to one case and removing spaces, accented letters, ampersands, etc. With Eloquent-Sluggable, you can easily create slugs for all the Eloquent models in your project.

Easy creation of slugs for your Eloquent models in Laravel.

Installation via composer

composer require cviebrock/eloquent-sluggable:^4.8
  1. The package will automatically register its service provider.
  2. Optionally, publish the configuration file if you want to change any defaults:
php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

Usage:

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

Saving a model is easy:

$post = new Post([
    'title' => 'My Awesome Blog Post',
]);

$post->save();

Leave a Reply

Your email address will not be published. Required fields are marked *