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

Ran ShemTov
Ran ShemTov
14,148 Points

Where 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

In 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?

I 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

<?php
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\Myer' not found

Hmmm.. In Myer.php have you added a namespace?

<?php class Myer extends Eloquent { }

This is my model so far, what would I need to add? Just a little confused!

When 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.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Myers extends Model { // }

Still no difference when create the model as so.

You 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.

I think I see your problem.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Myer extends Model {

    //

}

and to use

<?php

namespace App\Http\Controllers;

use App\Myer;

class SomeController extends BaseController {

    /**
     * Show the thing
     *
     * @return Response
     */
    public function show()
    {
        $myers = Myer::all();

        return view('myers.show', compact('myers'));
    }

}

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

<?php

use App\Myer;

// instead of

use App\Myers;

Thanks 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

If you have persistant problem with things, try running:

composer dump-autoload

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

You 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.

Only 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

Thanks Tom! Im gonna read about this.