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 trialRan ShemTov
14,148 PointsWhere should i place "TodoList.php" model in Laravel 5?
So I'm using Laravel 5. and since the models can be located anywhere, It can't read it right when I follow along with the video.
laravel.dev shows an error trying to find the class "TodoList". I tried many solutions on the web (such as directing with "use") and so on. I keep getting the message "Class 'App\Models\TodoList' not found" when directing to the models directory, or: "Class 'App\Http\Controllers\TodoList' not found" when not directing.
3 Answers
thomascawthorn
22,986 PointsIn Laravel 5, models are defaulted to the app directory. You'll spot User.php in this location. This is also where you should create TodoList.php.
When you reference it using the 'use' statement:
<?php
use app\Model;
// e.g.
use app\User;
use app\TodoList;
Does that help?
Patricio Di Muzio
Front End Web Development Techdegree Student 22,008 PointsYou got to modify the file /composer.json adding on "classmap" the path where you gonna generate new models. Then, on the console in your Vagrant machine use: "php artisan make:model TodoList". Reference it with 'use' statement like Tom says. And that's all. It works for me. Late, but maybe helps someone else.
thomascawthorn
22,986 PointsOnly in Laravel 4.1 would you have to modify the composer file ;)
Laravel 5 (or Laravel 5.1 now - make sure you upgrade!) comes with the app directory automatically psr-4'd. It should look like this out of the box.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
When composer sees psr-4, it assumes you are autoloading in a certain format. You therefore don't need to define the specific location of your models directory. Read more about psr-4
Patricio Di Muzio
Front End Web Development Techdegree Student 22,008 PointsThanks Tom! Im gonna read about this.
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 PointsI wondered if you could help me to.
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
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\Myer' not found
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsHmmm.. In Myer.php have you added a namespace?
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 Points<?php class Myer extends Eloquent { }
This is my model so far, what would I need to add? Just a little confused!
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 PointsWhen I add use app\Myer;
I get the following error:
Cannot declare class Myer because the name is already in use
I am so confused with this now.
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 Points<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Myers extends Model { // }
Still no difference when create the model as so.
Patricio Di Muzio
Front End Web Development Techdegree Student 22,008 PointsPatricio Di Muzio
Front End Web Development Techdegree Student 22,008 PointsYou tried generating the file with php artisan?
"php artisan app:name Myer"
and you can look in that template if the names are correctly added.
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsI think I see your problem.
and to use
This set up should work fine. The example you gave you're using a plural of Myer (Myers) in your use statement. Unless you use an alias, make sure the two match up!
so try
Max Smith
Courses Plus Student 3,973 PointsMax Smith
Courses Plus Student 3,973 PointsThanks for all the help, in the end I wrote the statement
myers = \App\Myer::all();
and it worked. I thought my hair was going to fall out but I got there in the end. Thanks a lot
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsIf you have persistant problem with things, try running:
This basically forces composer to delete and refresh it's class map (the thing it uses to find where classes are quickly).
Have a go at importing it at the top of the class, I'm intreagued to see if it works! Or don't :p