Earlier one of my laravel project used id
as route key name. I have found, slug
is more readable/memorable.
So instead of using id
I started to use slug
as route key name.
laravel-sluggable
While create a post, we can generate a slug by our own. To do this we can use Str::slug
method and append some unique value to make it unique.
But using laravel-sluggable, we can generate slug automatically
First we will install laravel-sluggable using composer
|
|
Let, Our model name is Post
and Posts
table has title
and slug
column. In this case, we need to
HasSlug
trait and define getSlugOptions
.
|
|
Inside getSlugOptions
method definition, we are generating slug. It will generate slug
from title
column.
we must import HasSlug
and SlugOptions
at top of the file.
getRouteKey
& getRouteKeyName
To make slug
as route key name we need add getRouteKey
and getRouteKeyName
definitions in our Post
model
|
|
Calling route
We are passing a $post
object directly as name route parameter, when using route method for url generation. Its automatically add slug as route key name
<a href="{{ route('post.show', $post) }}">
{{ $post->title }}
</a>
route('post.show', $post)
output will be like following http://domain.test/post/demo-post-slug
. Here getRouteKeyName
play the role to change to slug
instead id
Full Model code
|
|