Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Model Class not found

Tags:

php

laravel

When starting to work with models I got the following error

Class Post not found`.

All I did:
- Created a Model with the command php artisan make:model
- Tried to get all entries from table posts with echo Post::all()

I used the following code:

router.php

Route::get('/posts', function(){
    $results = Post::all();
    return $results;
});

Post.php

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    protected $table = 'posts';    
}

What I tried
- Renaming Class
- Dump-autoload (Laravel 4 Model class not found)

like image 989
Sylnois Avatar asked Feb 05 '15 17:02

Sylnois


4 Answers

Laravel 5 promotes the use of namespaces for things like Models and Controllers. Your Model is under the App namespace, so your code needs to call it like this:

Route::get('/posts', function(){

        $results = \App\Post::all();
        return $results;
});

As mentioned in the comments you can also use or import a namespace in to a file so you don't need to quote the full path, like this:

use App\Post;

Route::get('/posts', function(){

        $results = Post::all();
        return $results;
});

While I'm doing a short primer on namespaces I might as well mention the ability to alias a class as well. Doing this means you can essentially rename your class just in the scope of one file, like this:

use App\Post as PostModel;

Route::get('/posts', function(){

        $results = PostModel::all();
        return $results;
});

More info on importing and aliasing namespaces here: http://php.net/manual/en/language.namespaces.importing.php

like image 106
Dan Smith Avatar answered Nov 18 '22 19:11

Dan Smith


I was having the same "Class [class name] not found" error message, but it wasn't a namespace issue. All my namespaces were set up correctly. I even tried composer dump-autoload and it didn't help me.

Surprisingly (to me) I then did composer dump-autoload -o which according to Composer's help, "optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production." Somehow doing it that way got composer to behave and include the class correctly in the autoload_classmap.php file.

like image 76
jlbang Avatar answered Nov 18 '22 20:11

jlbang


I had the same error in Laravel 5.2, turns out the namespace is incorrect in the model class definition.

I created my model using the command:

php artisan make:model myModel

By default, Laravel 5 creates the model under App folder, but if you were to move the model to another folder like I did, you must change the the namespace inside the model definition too:

namespace App\ModelFolder;

To include the folder name when creating the model you could use (don't forget to use double back slashes):

php artisan make:model ModelFolder\\myModel
like image 8
Wei Avatar answered Nov 18 '22 18:11

Wei


I had this same issue but it just turns out that composer creates a separate Models folder for Laravel 8.*, so instead of

use App\Post;

I had to write:

use App\Models\Post;
like image 2
iamalminko Avatar answered Nov 18 '22 20:11

iamalminko