Download the PHP package taylornetwork/model-slugger without Composer
On this page you can find all versions of the php package taylornetwork/model-slugger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download taylornetwork/model-slugger
More information about taylornetwork/model-slugger
Files in taylornetwork/model-slugger
Package model-slugger
Short Description Automatically add slugs to eloquent models
License
Informations about the package model-slugger
ModelSlugger
Install
Using Composer
Usage
In a class that extends Illuminate\Database\Eloquent\Model
add the TaylorNetwork\ModelSlugger\ModelSlugger
trait.
This will require you to define the sluggerConfig()
function which returns an array with minimum options being ['source' => 'model field to make slug from']
Where name
would be converted to a slug and placed into the column slug
by default
Bind slug to route
You can bind the routes in your application to the slug rather than ID by adding
To your model, it will cause the routes to be looked up using the slug column.
Unique
To make all slugs unique add 'unique' => 'all'
to the config array either in the model or in config/slugger.php
Unique to a Parent
If you want slugs to only be unique based on a parent class, add 'unique' => 'parent', 'parent' => 'App\ParentClassName'
to the model or config/slugger.php
For example you have a App\User
model and a App\TodoList
model where the a user can have many todo lists each with slugs. If we set the config to unique => all
and every user makes a todo list named 'my todo list'
the slugs will become increasingly long as they become unique.
To avoid this you can make the slugs unique if they are from the same parent.
Slugger Route Model Bind with Unique to a Parent
To accomplish route model binding you will need to add the code to find only slugs with the correct parent to your App\Providers\RouteServiceProvider
class
For example
Credits
- Main Author: Sam Taylor
License
The MIT License (MIT). Please see License File for more information.