我試圖在我的路線中使用slug而不是id,但它回傳了 404 回應。我已經嘗試了所有可以使用的資源。請幫我。這是我的網址
<a href="{{route('blog.show' , $blog->id)}}">
<button id="readmore" class="btn-primary
mb-5 btn-xl">Read More</button></a>
這是我的控制器操作:
public function showPost(Blog $blog)
{
$comments = $blog
->comments()
->with(['blogs'])
->paginate(5);
return view('web.single-post', [
'blog' => $blog,
'comments' => $comments,
]);
}
這是我的路線定義:
Route::get('/blog/{blog:slug}', [
App\Http\Controllers\WelcomeController::class,
'showPost'
])->name('blog.show');
最后,這是我的模型:
public function getRouteKeyName()
{
return 'slug';
}
uj5u.com熱心網友回復:
更改{{route('blog.show' , $blog->id)}}為{{route('blog.show' , $blog->slug)}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/399379.html
