我正在尝试显示使用slug的帖子,但始终找不到404。
控制者
public function show($slug)
{
$post = Post::findorFail($slug);
$tags = Tag::all();
return view('page.detail', compact('post', 'tags'));
}
路线
Route::get('/post/{slug}', 'PageController@show');
视图
@foreach($post as $item)
<a href="/post/{{$item->slug}}" class="btn-custom">Read More <span
class="ion-ios-arrow-forward">
@endforeach
谢谢
findOrFail()
检查Post.php
模型的主键(通常为)id
,因此您的查询当前为:
SELECT * FROM posts WHERE id = 'contents-of-slug'
如果执行以下操作,您的代码应该可以工作:
$post = Post::where('slug', $slug)->firstOrFail();
请参阅文档;它显示了这个确切的示例:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句