I am trying to use slug in my route instead of the id but it is returning a 404 response. I have tried all resources I could use. Please help me. This is my web URL
<a href="{{route('blog.show' , $blog->id)}}">
<button id="readmore" >Read More</button></a>
This is my controller action:
public function showPost(Blog $blog)
{
$comments = $blog
->comments()
->with(['blogs'])
->paginate(5);
return view('web.single-post', [
'blog' => $blog,
'comments' => $comments,
]);
}
This is my route definition:
Route::get('/blog/{blog:slug}', [
App\Http\Controllers\WelcomeController::class,
'showPost'
])->name('blog.show');
Finally, this is my model:
public function getRouteKeyName()
{
return 'slug';
}
CodePudding user response:
Change {{route('blog.show' , $blog->id)}}
to {{route('blog.show' , $blog->slug)}}