RouteServiceProvider无法找到查询
问题描述:
我正在试图使seo友好的网址与邮件的slu make。那么,到目前为止,我还做这是我RouteServiceProvider
RouteServiceProvider无法找到查询
这在我BlogController
public function show(Post $post){
return view("blog.show", compact('post'));
}
当我去到后http://example.com/slug-from-database我已经得到了错误
ModelNotFoundException in Builder.php line 426:
没有关于型号[App \ Post]的查询结果。
任何想法,为什么我得到这个错误?
答
你在你的WHERE
条款缺失$slug
,在这里:
Route::bind('post', function($slug){
return Post::published()->where('slug')->first();
});
改变这样的查询,也将努力:
Post::published()->where('slug', $slug)->first();
只是想检查一下,你有一个叫做模型'Post'与'App \ Post'的命名空间? – Spholt
是的,我有。我也可以发布它,但我不在这里与问题有关。 –
我注意到你在服务提供者中指定了一个命名空间,可以试着在'App \ Http \ Controllers \ Post'中找到你的'Post'模型吗?没有更多的错误输出,有点难以分辨。这是一个很长的一段时间tbh – Spholt