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 trialMax Smith
Courses Plus Student 3,973 PointsEloquent 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?
:( :( :(
5 Answers
Corey Cramer
9,453 PointsFor fun try this:
public function index() {
$myers = \App\Myer::all();
return view('myers.index')->with('myers'.$myers);
}
Max Smith
Courses Plus Student 3,973 PointsWow, that worked. I am curious.
Why do I need to add the \App in Laravel 5 ?
Max Smith
Courses Plus Student 3,973 PointsAlso, 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?
Corey Cramer
9,453 PointsIn 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";
Max Smith
Courses Plus Student 3,973 PointsThanks Corey, you have been great. Makes a lot of sense now.
Corey Cramer
9,453 PointsA 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
39,021 PointsRan 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:
<?php
namespace App; // new
use Eloquent; // new
class TodoList extends Eloquent {}
Robert Young
11,742 PointsGreat, 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).
Corey Cramer
9,453 PointsAdding 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]
jasonnagler
4,934 PointsThis helped me too. Thanks!
Julian Cortes
3,549 PointsThank you guys !!! I was stuck with this.... I had to put $todo_lists = \App\TodoList::all(); ... and it worked
Anthony c
20,907 PointsThis still won't work for me....
<?php
namespace App
class TodoList extends Eloquent {}
<?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()
{
//
}
Corey Cramer
9,453 PointsAnthony,
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
14,631 PointsThanks 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!
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 PointsFatalErrorException in MyersController.php line 24: Class 'app\Myer' not found
FML FML FML