ifranksmith/laravel-sluggable

Laravel slug maker trait

dev-main 2021-09-21 11:03 UTC

This package is auto-updated.

Last update: 2024-04-21 16:43:59 UTC


README

Allows Laravel Eloquent models to implement a 'sluggable' feature.

Index

Installation

  1. Install the package via Composer
$ composer require ifranksmith/laravel-sluggable
  1. In Laravel >=5.5 this package will automatically get registered. For older versions, update your config/app.php by adding an entry for the service provider.
'providers' => [
    // ...
    IFrankSmith\Sluggable\SluggableServiceProvider
];

Usage

Your models should import the Traits/Sluggable.php trait and use it. (see an example below):

use IFrankSmith\Sluggable\Traits\Sluggable;

class Posts extends Model
{
	use Sluggable;
}

By default, this package will generate slug from the 'name' property of the mmodel and save it to the 'slug' property of the model.

However, if your model does not use those properties, you need to create a method on your model that returns the respective column and source. For example, if you want to make slug from 'title' to 'title_slug' property, you need to use it as shown below

use IFrankSmith\Sluggable\Traits\Sluggable;

class Posts extends Model
{
    use Sluggable;
    
    public function sluggable()
    {
        return [
            "column" => "title_slug",
            "source" => "title",
        ];
    }
}

That's it ... your model is now "sluggable"!