I am studying Laravel 5 as my new framework and I am following the video in Laracast and I got some weird error. I am displaying simple view in my controller but all I got is this error:
ModelNotFoundException in Builder.php line 125: No query results for model [App\Article].
Here's a bit 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 just confused because when I try to access the create() method the Laravel also read the show() method.
Is this correct? So in the route list the Laravel will read it's routes from top to bottom?
So in my route in order to prevent the error I should put first the route of create before the show?
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');
ModelNotFoundException is a db exception fired from the findOrFail method if the model your trying to find don't exist
check this redirect-if-model-doesnt-exists-modelnotfoundexception-doesnt-work-for-me
Because the /create also triggers the show url /{any}
You should do what you said, put the create route over the show route, or delegate that boring job using
Route::resource ('article', 'ArticleController')
Therefore you are getting a modelNotFoundException, because you are looking for an article with id = 'create'
Related
Below is my web.php content
Route::get('/myRoutes', function () {return view('myRoutes.routeIndex');})->middleware('auth');
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');
Route::get('/myRoutes/create', 'routes#create');
Below is my resource controller(route.php)
<?php
namespace App\Http\Controllers;
use App\route;
use Illuminate\Http\Request;
class routes extends Controller
{
public function create()
{
return view('myRoutes.routeCreate');
}
public function show(route $route)
{
return view('myRoutes.routeShow', compact('route'));
}
?>
Now if i visit mydomain.com/myRoutes, it gives me a list of routes available.(working as expected)
If I visit mydomain.com/myRoutes/1, it gives me the record with id=1(working as expected)
Now the issue is if I visit mydomain.com/myRoutes/create, it returns a 404 error that the page not found. If I comment out the following line in web.php
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');
then mydomain.com/myRoutes/create works as expected. I think laravel is confused between /myRoutes/{id} and /myRoutes/create. How to fix this?
in your web.php ,add create route before your show route.
Route::get('/myRoutes', function () {return view('myRoutes.routeIndex');})->middleware('auth');
Route::get('/myRoutes/create', 'routes#create');
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');
Try putting your create route before your show route in web.php
When I remove the inital use Illuminate\Http\Request and add use App\Item instead in the Controller file, the items/create route responds with a 404. How can I still use the App\Item namespace and get to the items/create route? I've tried adding both, but does not work.
web.php
Route::get('items', 'ItemsController#index');
Route::get('items/{item}', 'ItemsController#show');
Route::get('items/create', 'ItemsController#create');
ItemsController.php
<?php
namespace App\Http\Controllers;
use App\Item;
class ItemsController extends Controller
{
public function index(){
$items = Item::all();
return view('items.index', ['items' => $items]);
}
public function show(Item $item){
return $item->body;
}
public function create(){
return view('items.create');
}
}
Item.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
//
}
The problem is that laravel tries to match the routes in the order they are declared and the items/{item} route will match all routes starting with items/, including items/create. And because of the route model binding, Laravel tries to load an Item with ID create which obviously doesn't exist, so it throws a 404 error.
Route model binding in the docs:
Since the $user variable is type-hinted as the App\User Eloquent model
and the variable name matches the {user} URI segment, Laravel will
automatically inject the model instance that has an ID matching the
corresponding value from the request URI. If a matching model instance
is not found in the database, a 404 HTTP response will automatically
be generated.
To fix it simply change the order of your routes and put items/{item} after all other item/* routes:
Route::get('items', 'ItemsController#index');
Route::get('items/create', 'ItemsController#create');
Route::get('items/{item}', 'ItemsController#show');
Can you try this, please.
<?php
namespace App\Http\Controllers;
use App\Item;
class ItemsController extends Controller
{
public function index(){
$items = Item::all();
return view('items.index', ['items' => $items]);
}
public function show($id){
$item= Item::find($id);
return $item->body;
}
public function create(){
return view('items.create');
}
}
I cant seem to get my edit function to work in my resourceful controller. This is my controller:
class UserController extends Controller{
public function index()
{
return view('testindex');
}
public function test(){
return 'test';
}
public function edit(User $user){
return 'test2';
}
public function create(){
return 'test3';
}
}
And my routes:
Route::post('test','UserController#test');
Route::resource('/','UserController');
Which mean that edit should be in the resource controller.
Create works but edit isn't, it gives me a
NotFoundHttpException
This is the form:
Edit
And yes, the variable $id works and is showing in the url.
What am I doing wrong here?
This is because you're not naming the resource i.e.
Route::resource('user', 'UserController');
To get around this you will need to change you route to be:
Route::resource('/', 'UserController', ['parameters' => [
'' => 'user'
]]);
(The above will allow you to keep your urls the same).
Please note that you will have to keep this Route at the bottom of your file.
Hope this helps!
I am new in Laravel. I am trying to create a new page named as "contact". But i am getting a Object not found error when i am trying to access the contact page
URL: project-name/contact
please help me
---routes file
<?php
Route::get('/','WelcomeController#index');
Route::get('contact','WelcomeController#contact');
Route::group(['middleware' => ['web']], function () {
//
});
--- Welcome controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class WelcomeController extends Controller
{
public function index(){
return view('welcome');
}
public function contact(){
return 'Contact page goes here...';
}
}
Set your home directory to the public to make things work.
Exchange the route. like this:
Route::get('contact','WelcomeController#contact');
Route::get('/','WelcomeController#index');
Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}