gpt4 book ai didi

Builder.php 中用于路由的 Laravel 5 ModelNotFoundException

转载 作者:行者123 更新时间:2023-12-04 22:11:25 25 4
gpt4 key购买 nike

我有一个名为 Article.php 的模型类
并使用以下路线:

Route::get('articles/create','ArticlesController@create');

在浏览器中输入时 http://localhost:8000/articles/create
我看到这个错误:
Builder.php 第 125 行中的 ModelNotFoundException:没有模型 [App\Article] 的查询结果。

但是当我用户低于每个想法都可以:(文章插入文章 s )
Route::get('article/create','ArticlesController@create');

这是我的 Controller :
class ArticlesController extends Controller {

public function index()
{
$articles = Article::all();

return view('articles.index',compact('articles'));
}


public function show($id)
{
$article = Article::findOrFail($id);

return view('articles.show',compact('article'));
}

public function create()
{
return view('articles.create');
}
}

到底发生了什么?!!!

最佳答案

您的代码的问题在于,在您的 routes.php 中,您的路线优先级是这样的:

Route::get('articles/{id}','ArticlesController@show');
Route::get('articles/create','ArticlesController@create');

并且当您在浏览器中访问 http://localhost:8000/articles/create 时,laravel 在 {id} 有机会解析路由之前,会在 articles/{id} 中通过 articles/create 请求创建一个变量。要解决您的问题,您必须考虑路由优先级并对 route.php 文件进行以下更改:
Route::get('articles/create','ArticlesController@create');
Route::get('articles/{id}/edit','ArticlesController@show');
Route::get('articles/{id}','ArticlesController@show');

但是如果你的 routes.php 文件中有一堆这些,你真的应该考虑使用它:
Route::resource('articles', 'ArticlesController');

这一行将处理所有 4 个获取路由(索引、创建、编辑、显示)以及所有三个 post/put/delete 路由(存储、更新、删除)。

但每个人都有自己的。

关于Builder.php 中用于路由的 Laravel 5 ModelNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com