How does the laravel process go?

I am learning Laravel 5 as my new framework and I am following a video in Laracast and I got some strange error. I am showing a simple view in my controller, but all I have is this error:

ModelNotFoundException in Builder.php line 125: No query results for model [App\Article].

      

Here's some of my code:

Route:

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

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

      

ArticlesController.php

<?php namespace App\Http\Controllers;

use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

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 'Hello World'; //display error messages

    }



}

      

So, I was just confused because when I try to access the create () method, Laravel reads the show () method as well.

It is right? So, in the list of routes, Laravel will read its routes from top to bottom?

So, on my way to prevent the error, I have to put the create route first before showing?

So it should be like this?

Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create'); //make it first?
Route::get('articles/{id}', 'ArticlesController@show');

      

+3


source to share


2 answers


ModelNotFoundException is a db exception thrown from a method findOrFail

if the model you are trying to find does not exist



check redirect-if-model-doesnt-exists-modelnotfoundexception-doesnt-work-for-me

+3


source


Since / create also runs show url / {any}

You have to do what you said, put the creation route through the show route, or delegate this boring work using



Route :: resource ('article', 'ArticleController')

So you get modelNotFoundException because you are looking for an article with id = 'create'

0


source







All Articles