Laravel Slug Urls
1 min read

Laravel Slug Urls

Sometimes you want your urls to have a better description of the content being shown. For instance, when viewing a blog made with Laravel, a posts url would be /posts/1. Where the 1 would be the id of the post record in the database. It would be ideal if the url could have a slug in it instead, ie /posts/this-is-a-post.

Doing this with laravel is SUPER easy, literally 1 line of code.

Updating Your Route

Assuming the record you want to show with a slug has a slug column in the database, all you need to do is update your route like so...

Route::get('/posts/{post:slug}', [PostController::class, 'show']);

Since were using model binding for this route, all we are doing is telling Laravel to query for the record with the slug column instead of the id column.