Allows Laravel Eloquent models to implement a 'sluggable' feature.
- Install the package via Composer
$ composer require ifranksmith/laravel-sluggable
- 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
];
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"!