48

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)

milo526
  • 5,012
  • 5
  • 41
  • 60
Sylnois
  • 1,589
  • 6
  • 23
  • 47

12 Answers12

87

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

Dan Smith
  • 5,685
  • 32
  • 33
  • 1
    Ouh wow, it worked. Thanks a lot. Another question: I want to create 5 Models. User, Post, UserSettings, PostVote, PostComment. Should I put them all under the App namespace? – Sylnois Feb 05 '15 at 18:00
  • Sure! In fact, you can put them under any namespace that makes sense to you - there aren't many limits. It's important that all models are under the *same* namespace though. (Actually they don't HAVE to be but it makes it easier if you're just getting started with them) – Dan Smith Feb 05 '15 at 18:04
  • And there is no chance to use shorted `Post::all();` instead of `\App\Post::all()` all the time? – Sylnois Feb 05 '15 at 18:07
  • Sure, two ways. You can remove the `namespace App;` bit from your model thus bringing it back in to a global scope, which is fine but namespaces are very useful in modern development and it's worth using them. The other way is to `use` (import) a namespace in to your file so you don't have to use the full path each time, like the `use Illuminate\Database\Eloquent\Model;` call in your model, for example. I'll update my answer to include that as an option. – Dan Smith Feb 05 '15 at 20:05
  • Well then, you already gave me the answer I needed. I will try to learn how to use namespaces. :) – Sylnois Feb 06 '15 at 10:39
79

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.

jlbang
  • 1,226
  • 12
  • 14
10

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
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
Wei
  • 724
  • 9
  • 10
4

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;
iamalminko
  • 105
  • 8
0

Make sure to be careful when editing your model file. For example, in your post model:

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;

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

You need to pay close attention to the class Post extend Model line. The class Post defined here will be your namespace for your controller.

Erich Meissner
  • 205
  • 2
  • 5
0

In your router.php file, you should use the model class like this

 use App\Models\Post;

and use the model class like this.

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

        $results = Post::all();
        return $results; });
Sheetal Mehra
  • 478
  • 3
  • 13
0

For me it was really silly I had created a ModelClass file without the .php extension. So calling that was giving model not found. So check the extension has .php

DragonFire
  • 3,722
  • 2
  • 38
  • 51
0

I had the same issue. It turned out my Post model was in a php file whose name was not Post.php

=> the filename must match the name of your class (Post class in Post.php file)

0

One reason for Class not found error is case sensitive of characters, so if namespace is namespace App\Model;, if you call it like App\model\User here error Class not found will shown due model != Model in name space.

AnasSafi
  • 5,353
  • 1
  • 35
  • 38
0

If you using OPCache, maybe in some reason your classes are not indexed in OPCache, so you have to reindex the ones by restarting PHP (example below is for PHP 8.1 FPM on Nginx and Ubuntu).

sudo service php8.1-fpm restart
Igor
  • 755
  • 1
  • 10
  • 22
-1

As @bulk said, it uses namespaces.

I recommend you to start using an IDE, it will suggest you to import all the required namespaces (\App\Post in this case).

Dmitry
  • 7,457
  • 12
  • 57
  • 83
-1

If after changing the namespace and the config/auth.php it still fails, you could try the following:

  1. In the file vendor/composer/autoload_classmap.php change the line App\\User' => $baseDir . '/app/User.php',, to App\\Models\\User' => $baseDir . '/app/Models/User.php',

  2. At the beginning of the file app/Services/Registrar.php change "use App\User" to "App\Models\User"

milo526
  • 5,012
  • 5
  • 41
  • 60
jpbourbon
  • 151
  • 1
  • 5
  • This is a very specific answer (only works for the default user, not general classes). Furthermore changing vendor files is a really bad idea since your changes will break when updating the framework. – milo526 Aug 14 '17 at 21:43