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

ModelNotFoundException in Laravel Basics project

The error is:

"Illuminate \ Database \ Eloquent \ ModelNotFoundException

No query results for model [TodoList]."

Which is strange because I definitely have the TodoList class in the model and as far as I can tell I've followed all the instructions to the letter.

The line that's generating the error is "$todo_lists = TodoList::all();" from my controller, under the create method. The issue only happens with the /create url, if I go to the root or /todos then I have no issues.

Here's the code for the class:

"<?php class TodoList extends Eloquent{}

"

I feel like the problem is something embarrassingly dumb.

4 Answers

Hi Alex,

Could you post your entire controller as you shouldn't be making a call to get all your todo items when your creating an item.

https://teamtreehouse.com/forum/posting-code-to-the-forum

Here ya go:

<?php

class TodoListController extends BaseController {


    public function index()
    {
        $todo_lists = TodoList::all();
        return View::make('todos.index')->with('todo_lists', $todo_lists);
    }



    public function create()
    {
        return View::make('todos.create');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        return "Create a new list";
        //$list = new TodoList();
        //$list->name = "Another List";
        //$list->save();
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {

        $list = TodoList::findOrFail($id);

        return View::make('todos.show')->withList($list);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }


}

Here are my routes:

<?php

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

Route::get('/db', function() {


    $result = DB::table('todo_lists')->where('name', 'Your List')->first();
    return $result->name;
});

Route::get('/todos/{id}', 'TodoListController@show');

Route::resource('todos', 'TodoListController');

What does your create view look like?

@extends('layouts.main');

@section('content')

{{Form::open( array('route'=> 'todos.store') )}}

{{ Form:close() }}
@stop