Faramarz Qoshchi
Faramarz Qoshchi

Reputation: 1632

Laravel: class App\Posts does not exists

I have got this error so many times in Laravel as using model classes that says this class does not exists which it does. one of them was when i ran App\Post::count() with tinker and got this:

PHP Fatal error: Class 'App/Post' not found in Psy Shell code on line 1

and one was when i tried to seed the DB with model. this is the model content:

namespace App;    
use Illuminate\Database\Eloquent\Model;   
class Dog extends Model
{   
    //   
} 

and this is the Seeder:

<?php
use Illuminate\Database\Seeder;
 use \App\Dog;   
class DogsTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
    public function run()
    {
        Dog::query()->create(['name'=>'jack']);
    }
}

with this error: Class 'App\Dog' not found.and this is for every model i create.
any solution? (Laravel -V: 5.4, PHP -V: 7.2, Apache webserver)

Upvotes: 2

Views: 6370

Answers (7)

Isaac Agyei Annor
Isaac Agyei Annor

Reputation: 21

I was having a similar problem with App\Post::count() but it works when I add Models before the Post. You can try that one too.

App\Models\Post::count()

So, in your case:

App\Models\Dog

Upvotes: 0

kalpana udara
kalpana udara

Reputation: 71

Don't use this path

App\Post

Use this path

App\Models\Post

Upvotes: 0

Joy Sarkar
Joy Sarkar

Reputation: 1

Use this path, hope you got your answer

App\Models\Post

Upvotes: 0

MoviesLiker
MoviesLiker

Reputation: 51

Use this path.

>>> App\Models\Post

Upvotes: 5

yassine dotma
yassine dotma

Reputation: 775

Sometimes it is necessary to clear the cache, this command worked in my case:

composer dump-autoload

I hope it works for you too

regards

Screenshot

Upvotes: 4

Илья Алисов
Илья Алисов

Reputation: 434

After trying to fix the same issue about half hour on laravel 5.8 with php v7.2, I noticed, that my php file started with <? output delimiter. Changing <? to <?php fixed my problem.

Upvotes: 2

Josh
Josh

Reputation: 63

On some servers you need to prefix App with \ like: namespace \App;

Also after the above change:

  • Also double check class name in model
  • run composer dump-autoload
  • php artisan cache:clear

Upvotes: 0

Related Questions