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 Laravel and Databases Eloquent ORM

Eloquent ORM made my soul escape and my hair fall out

I have a wall with this...hard!

I am using Laravel 5 and have placed my model under app, where User.php is

I have small differences with the course, instead of TodoList, I am using the word Myer

I have a MyersController with the following

public function index() { $myers = Myer::all(); return view('myers.index')->with('myers'.$myers); }

which is accessed at /myer Route::get('/myer/', 'MyersController@index');

When I try to access the page, I get the following error.

FatalErrorException in MyersController.php line 19: Class 'App\Http\Controllers\Myer' not found

Why why why why why?

:( :( :(

FatalErrorException in MyersController.php line 24: Class 'app\Myer' not found

FML FML FML

5 Answers

For fun try this:

public function index() {
    $myers = \App\Myer::all();
    return view('myers.index')->with('myers'.$myers);
}

Wow, that worked. I am curious.

Why do I need to add the \App in Laravel 5 ?

Also, I am a little bit confused. If I had multiple tables in my db. How would Laravel know which one to pull from?

Like where is the connection made that says that this relates to this table?

In regards to the first part: I don't know why really, I still have a lot of learning to do myself before that makes more sense.

The second part: Unless otherwise stated Laravel assumes that you name your model after the table in your database. So model Myers is for table myers, Users is users, SuperHappyFunTime becomes super_happy_fun_time.

If you want it to be something else inside of your model you need to put:

protected $table = "table_name";

Thanks Corey, you have been great. Makes a lot of sense now.

A quick amendment because my response was slightly incorrect. Model names should always be singular so where I put Users as the model it should be User for users, Myer for myers and SuperHappyFunTime would be super_happy_fun_times.

Also you're very welcome!

Greg Kaleka
Greg Kaleka
39,021 Points

Ran into the same issue. Did a good amount of googling. Solved it.

For anyone else getting the error "Class 'App\TodoList' not found", the solution is to add the following two lines of code to the top of TodoList.php:

TodoList.php
<?php
namespace App;  // new
use Eloquent;   // new

class TodoList extends Eloquent {}

Great, this worked for me, but like Corey suggested, I had to put $todo_lists = \App\TodoList::all(); in my controller file, instead of $todo_lists = TodoList::all(); (Hampton's original suggestion).

Adding use Eloquent also didn't work for me. If you would like to save some head space the other solution I found is to add:

<?php
use App\[Model Name]

This helped me too. Thanks!

Julian Cortes
Julian Cortes
3,549 Points

Thank you guys !!! I was stuck with this.... I had to put $todo_lists = \App\TodoList::all(); ... and it worked

Anthony c
Anthony c
20,907 Points

This still won't work for me....

laravel-basics/app/models/TodoList.php
<?php


namespace App


class TodoList extends Eloquent {}
laravel-basics/app/Http/Controllers/TodoListController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

class TodoListController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $todo_lists = App/TodoList::all();
        return view('todos.index')->with('todo_lists', $todo_lists);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

Anthony,

Sorry I never got back to you sooner and necro'ing this might not be the best but you are looking for backslash, not forward. That should fix the issue you had and save a bunch of headaches on your coding journeys.

Colin Sharkey
Colin Sharkey
14,631 Points

Thanks Corey Cramer,

<?php use App\TodoList; // in the TodoListController.php file

This worked for me the other ways didn't... So close to giving up!