Laravel 5 ModelNotFoundException in Builder.php for routing
I have a model class named Article.php and use below route:
Route::get('articles/create','ArticlesController@create');
when typing in the browser http: // localhost: 8000 / articles / create I see this error: ModelNotFoundException at line Builder.php 125: No query results for model [App \ Article].
but when the user below each one thinks ok: (insted article s )
Route::get('article/create','ArticlesController@create');
this is my 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');
}
}
what really happened? !!!
source to share
The problem with your code is that in your routes.php the priority of your route is like this:
Route::get('articles/{id}','ArticlesController@show');
Route::get('articles/create','ArticlesController@create');
and when you go to http: // localhost: 8000 / articles / create in your browser, laravel catches, create as a variable the request in before gets able to resolve the route. To solve your problem, you should consider route priority and make the following changes to your route.php file: {id}
articles/{id}
articles/create
Route::get('articles/create','ArticlesController@create');
Route::get('articles/{id}/edit','ArticlesController@show');
Route::get('articles/{id}','ArticlesController@show');
But if your routes.php file has its own group, you should think about this:
Route::resource('articles', 'ArticlesController');
This single line will take care of all 4 routes (indexes, create, edit, show) as well as all three post / put / delete routes (save, update, delete).
But to each his own.
source to share
I found the question: in Rout.php I have below code:
Route::get('articles/{id}','ArticlesController@show');
Route::get('articles/create','ArticlesController@create');
and when i use http: // localhost: 8000 / articles / create in browser laravel pass create in article / {id} ' and use Routing first and then ArticlesController @show and passe ' create ' instead of id how to show . then we cannot find the article with id = create and return an exception;
to solve the problem, you need to change the sequence of routes as shown below:
Route::get('articles/create','ArticlesController@create');
Route::get('articles/{id}','ArticlesController@show');
source to share