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 Laravel 4 Basics Relating Data Accessing our Relationships

FatalErrorException in Model.php line 857: Class 'TodoItem' not found

Hi!

I am following the Laravel Basics, and using Laravel 5, I have been writing some other code that Hampton as they've changes.

But after creating a new model todoitem.php using "php artisan make:model" and changing the content as per the video, I get the following message: "FatalErrorException in Model.php line 857: Class 'TodoItem' not found".

I have included use "App\TodoItem;" in the file TodoListController.php, but that don't help.

How do I get this fixed? Thanks in advance :)

As a sidenote; I am on the section "Accessing our Relationships".

Benjamin Payne
Benjamin Payne
8,142 Points

Hey Erik, Can you include the code here by forking your workspace here. Also try adding a ' \ ' in front of App\TodoItem;

so

\App\TodoItem

Let me know.

Best, Ben

Here is the code in ToDoListController.php:

            <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\TodoList;
use \App\TodoItem;
use Input;
use Illuminate\Support\Facades\Validator;

class TodoListController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $todo_lists = TodoList::all();
        return View('todos.index')->with('todo_lists', $todo_lists);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View('todos.create');
    }

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

        // Define rules
        $rules = array(
            'name' => array('required', 'unique:todo_lists')
        );

        // pass input to validator
        $validator = Validator::make(Input::all(), $rules);

        // test if input is valid
        if ($validator->fails()) {

            return redirect()->route('todos.create')->withErrors($validator)->withInput();
        }

        $name = Input::get('name');
        $list = new TodoList();
        $list->name = $name;
        $list->save();

        //return Redirect('todos.index');
        return redirect()->route('todos.index')->withMessage('List was created!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $list = TodoList::findOrFail($id);
        $items = $list->listItems()->get();
        return $items;
        return view('todos.show')->withList($list);
    }

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

        $list = TodoList::findOrFail($id);
        return view('todos.edit')->withList($list);
    }

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

        // Define rules
                $rules = array(
                    'name' => array('required', 'unique:todo_lists')
                );

                // pass input to validator
                $validator = Validator::make(Input::all(), $rules);

                // test if input is valid
                if ($validator->fails()) {

                    return redirect()->route('todos.edit', $id)->withErrors($validator)->withInput();
                }

                $name = Input::get('name');
                $list = TodoList::findOrFail($id);
                $list->name = $name;
                $list->update();

                //return Redirect('todos.index');
                return redirect()->route('todos.index')->withMessage('List was updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $todo_list = TodoList::findOrFail($id)->delete();
        return redirect()->route('todos.index')->withMessage('Item deleted!');
    }
}

And the code in TodoItem.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TodoItem extends Model
{
    public function todoList() {
        return $this->belongsTo('TodoList');
    }
}

I tried adding a \ before App, but that didnt work.

2 Answers

Hey Erik, I had this exact error for the last hour. I finally resolved it by revising my TodoList.php file. I added App\ to the 'TodoItem' in the return statement.

Return statement after revision:

return $this->hasMany('App\TodoItem');

Hope this helps!

Kent, you are a godsend. I have been following on with these videos too, and came a cropper at the same error, and I was stuck for about a day or so. (You may think they might, well, update the tutorial at some point, but things take time I guess!)

Thankyou :)

Happy to help Robert! I've hit many snags working with this framework. I know how awesome it is to see that someone has posted their solution to the same problem and it gets you out of a bind! Good Luck!

Kent is a HERO!