简体   繁体   中英

how to use slug in my route instead of an id

** I am trying to use slug in my route instead of the id but it is returning 404 error. 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" class="btn-primary 
mb-5 btn-xl">Read More</button></a> 
  • this is my controller*
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





Route::get('/blog/{blog:slug}', 
[App\Http\Controllers\WelcomeController::class, 'showPost'])- 
>name('blog.show');


finally this is my model


 public function getRouteKeyName()
{
    return 'slug';
}

Change {{route('blog.show', $blog->id)}} to {{route('blog.show', $blog->slug)}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM