Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Daniel Malek
Daniel Malek
20,140 Points

No query results for model Error

when adding new route create got Error.

Sorry, the page you are looking for could not be found. 2/2 NotFoundHttpException in Handler.php line 131: No query results for model [App\RamCar] create

My CarsListController file looks like this:

class CarsListController extends Controller
{

    public function index()
    {

    $ram_cars = \App\RamCar::all();
    return View::make("cars.index")->with('ram_cars', $ram_cars);
    }

    public function show($id)
    {
    $car = \App\RamCar::findOrfail($id);
    return View::make('cars.show')->withCar($car);
    }

    public function create()
    {

    return View::make('cars.create');
    }
}

web file

Route::get('/', 'CarsListController@index');
Route::get('/cars', 'CarsListController@index');
Route::get('/cars/{id}','CarsListController@show');
Route::get('/cars/create','CarsListController@create');

Route::get('/db',function()
{
    $result = DB::table('ram_cars')->where('owner','daniel')->first();
    return $result->owner;
});


Route::resource('cars','CarsListController');

create.blade.php

@extends('layouts.main')
@section('content')
    <h2>create</h2>
@stop

RamCar.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class RamCar extends Model {}

thats my routes

+--------+-----------+-----------------+--------------+-------------------------------------------------+--------------+
| Domain | Method    | URI             | Name         | Action                                          | Middleware   |
+--------+-----------+-----------------+--------------+-------------------------------------------------+--------------+
|        | GET|HEAD  | /               |              | App\Http\Controllers\CarsListController@index   | web          |
|        | GET|HEAD  | api/user        |              | Closure                                         | api,auth:api |
|        | GET|HEAD  | cars            | cars.index   | App\Http\Controllers\CarsListController@index   | web          |
|        | POST      | cars            | cars.store   | App\Http\Controllers\CarsListController@store   | web          |
|        | GET|HEAD  | cars/create     | cars.create  | App\Http\Controllers\CarsListController@create  | web          |
|        | GET|HEAD  | cars/{car}      | cars.show    | App\Http\Controllers\CarsListController@show    | web          |
|        | PUT|PATCH | cars/{car}      | cars.update  | App\Http\Controllers\CarsListController@update  | web          |
|        | DELETE    | cars/{car}      | cars.destroy | App\Http\Controllers\CarsListController@destroy | web          |
|        | GET|HEAD  | cars/{car}/edit | cars.edit    | App\Http\Controllers\CarsListController@edit    | web          |
|        | GET|HEAD  | cars/{id}       |              | App\Http\Controllers\CarsListController@show    | web          |
|        | GET|HEAD  | db              |              | Closure                                         | web          |
+--------+-----------+-----------------+--------------+-------------------------------------------------+--------------+

not sure when I made a mistake Big thanks for any clues.

Alena Holligan
Alena Holligan
Treehouse Teacher

according the the message "No query results for model " it sound like it can't retrieve any results from the database

4 Answers

Daniel Malek
Daniel Malek
20,140 Points

The Answer is:

Route::resource('cars','CarsListController'); has to be on beginning of the file

Daniel Malek
Daniel Malek
20,140 Points

I can connect to database and I can read it and display results. But when I try to do next step and start write to database I have a problem. I create new route (following same pattern) "create" and even if I'm not trying to do anything with database just to find out is route working with simple message I got this error. I can do any new route like /db or /anything and display message. Routes /cars or /cars/1 works fine. Route /cars/create give me the error.

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

so you are having problems when trying to "create" a car? First I believe if you want to change the database at all, your "method" should be "POST" not "GET".

Route::POST('/cars/create','CarsListController@create');

From your route results, it looks like you have a cars.store route that is using POST? Is that creating a record? I don't see the code for that route. Can you publish your code to github?

Daniel Malek
Daniel Malek
20,140 Points

I'm very grateful for your help. I enjoyed your php course a LOT :) by following it I did my firs app witch stores info about cars and owners and display alerts when some conditions are met. This project is a try to make it even better. Code is a follow up laravel 4 course. But in laravel 5 syntax (main reason I have so much trouble)

https://github.com/Danbass07/ram

I need to display form in order to "create" new car so on that stage I do not even trying write anything to database. Just simply create route and display simple message in create.blade.php.

Daniel Malek
Daniel Malek
20,140 Points

I'm very grateful for your help. I enjoyed your php course a LOT :) by following it I did my firs app witch stores info about cars and owners and display alerts when some conditions are met. This project is a try to make it even better. Code is a follow up laravel 4 course. But in laravel 5 syntax (main reason I have so much trouble)

https://github.com/Danbass07/ram

I need to display form in order to "create" new car so on that stage I do not even trying write anything to database. Just simply create route and display simple message in create.blade.php.