65

As soon, I am typing php artisan db:seed command.

I'm getting Error Like:

[ReflectionException]
Class UserTableSeeder does not exist

root@dd-desktop:/opt/lampp/htdocs/dd/laravel# php artisan db:seed

Here, Is my UserTableSeeder.php & DatabaseSeeder.php Page

UserTableSeeder.php

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class UserTableSeeder extends Seeder
{    
    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
        'name'     => 'Chris Sevilleja',
        'username' => 'sevilayha',
        'email'    => 'chris@scotch.io',
        'password' => Hash::make('awesome'),
        ));
    }    
}

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();
        $this->call('UserTableSeeder');
    }
}

I'm Referring This Link To Design & Develop Login Page. Please Help me to resolve this issue. Thanks.

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • which version of laravel are you using ? – Aatif Akhter Sep 09 '15 at 10:16
  • 1
    Laravel 5.0 Mr @Aatif. Will It Matter For Running Any Artisan Command. – Nana Partykar Sep 09 '15 at 10:20
  • 1
    Let me try run it from my end. I will let you know the outcome. – Aatif Akhter Sep 09 '15 at 10:22
  • Possible duplicate of [Laravel 5 - artisan seed \[ReflectionException\] Class SongsTableSeeder does not exist](http://stackoverflow.com/questions/26143315/laravel-5-artisan-seed-reflectionexception-class-songstableseeder-does-not-e) – Organic Advocate Nov 30 '15 at 18:56
  • My problem was that the class was used with a different casing than its definition, for example: myTestClass vs MyTestClass. I did not notice the problem until I was in production because I have a different filesystem locally than in production – coccoinomane Oct 08 '18 at 19:22
  • From my experience this is mostly due to an error in specifying the location or names of things. In my case it was a typo in the class name – Robert Jun 03 '20 at 00:43

23 Answers23

137

Perform a composer update, then composer dump-autoload.

If the above doesn't solve the problem, change the classmap in your composer.json file such that it contains the project-relative path to your php files:

"autoload-dev": {
    "classmap": [
        "tests/TestCase.php",
        "database/seeds/UserTableSeeder.php" //include the file with its path here
    ]
}, /** ... */

and soon after, perform a composer dump-autoload, and it should work now like a breeze!

Edit by @JMSamudio

If composer dump-autoload is not found, just enable this option composer config -g -- disable-tls true.

Community
  • 1
  • 1
Ahmad Baktash Hayeri
  • 5,802
  • 4
  • 30
  • 43
  • Hi Ahmad. 1) root@jbkinfotech-desktop:/# $php composer.phar update 2) root@jbkinfotech-desktop:/opt/lampp/htdocs/danish/laravel# $php composer.phar update Both giving composer.phar command not found. Sorry. I am very new to laravel. Don't Know Much Command. I Found Composer.json file. There I Edited. But, How To write command for dump-autoload command. – Nana Partykar Sep 09 '15 at 09:55
  • What happens if you type `composer` and press enter in the bash, in the _root_ of your project. i.e. for example your project is in `htdocs/danish/myProject`, execute it here. – Ahmad Baktash Hayeri Sep 09 '15 at 09:58
  • root@danish-desktop:/opt/lampp/htdocs/danish/laravel# composer. It display Usage, Available Commands – Nana Partykar Sep 09 '15 at 10:01
  • root@jbkinfotech-desktop:/opt/lampp/htdocs/danish/laravel# composer dump-autoload It shows "Generating autoload files" – Nana Partykar Sep 09 '15 at 10:03
  • 1
    Now, php artisan db:seed shows new error [Symfony\Component\Debug\Exception\FatalErrorException] Class 'User' not found – Nana Partykar Sep 09 '15 at 10:05
  • That means your `UserTableSeeder.php` is in fact loaded, check where you have used class `User`, refer to it with the correct namespace. – Ahmad Baktash Hayeri Sep 09 '15 at 10:08
  • Hi Ahmad, I have no where declared class with class name 'User'. Error line showing in this line "User::create(array(" <<---- in UserTableSeeder.php. Sorry. – Nana Partykar Sep 09 '15 at 10:32
  • 3
    Bingo, I think the line should be `App\User` or `App\Models\User` if you have your `User` model in `Models` folder. – Ahmad Baktash Hayeri Sep 09 '15 at 10:58
  • @AhmadBaktashHayeri can you explain what both commandos do exactly in light of the OP's question? – Daan Jul 04 '16 at 15:59
24

From my experience, this will show up most of the time when the class you are trying to call has some bugs and cannot be compiled. Check if the class that is not being reflected can be executed at its own.

Kamaro
  • 955
  • 1
  • 10
  • 11
19

A composer dump-autoload should fix it.

fsavina
  • 370
  • 4
  • 10
5

I had this problem and I could solve it by doing php artisan config:cache. The problem was that I had already run that command previously and later included some new seeder classes. The cached configurations didn't recognize the new classes. So running that command again worked.

If you see yourself making frequent changes to include new seeder classes then consider running php artisan config:clear. This will enable you to make as many changes as you'd like and then after testing you can run config:cache again to make things run optimally again.

Adam Ranganathan
  • 1,691
  • 1
  • 17
  • 25
5

I have the same problem with a class. I tried composer dump-autoload and php artisan config:clear but it did not solve my problem.

Then I decided to read my code to find the problem and I found the problem. The problem in my case was a missing comma in my class. See my Model code:

{
    protected
    $fillable = ['agente_id', 'matter_id', 'amendment_id', 'tipo_id'];

    public
    $rules = [
        'agente_id' => 'required', // <= See the comma
        'tipo_id' => 'required'
    ];

    public
    $niceNames = [
        'agente_id' => 'Membro', // <= This comma is missing on my code
        'tipo_id' => 'Membro'
    ];
}
Worthwelle
  • 1,244
  • 1
  • 16
  • 19
Luciano Braga
  • 253
  • 1
  • 3
  • 9
  • 2
    I can't believe how a little error like this can cause such a misleading error message, but yes, it happens. Thanks, you probably saved me a couple hours with this. – MetalCraneo May 11 '18 at 00:50
  • Me too. Looking at the code with scrutiny turned out the last single quote was missing 'tipo_id' => ```'required,``` instead of the correct ```'required',```. Thanks for the guidance. – Lod Apr 27 '20 at 19:58
5

Check your capitalization!

Your host system (Windows or Mac) is case insensitive by default, and Homestead inherits this behavior. Your production server on the other hand is case sensitive.

Whenever you get a ClassNotFound Exception check the following:

  1. Spelling
  2. Namespaces
  3. Capitalization
vimuth
  • 5,064
  • 33
  • 79
  • 116
Fazil Raza
  • 141
  • 2
  • 6
4

For some reason I had to run composer dumpautoload -o. Notice flag -o which mean with optimization. I don't have any idea why it works only with it but give it a try. Perhaps it helps someone.

Jaroslav Klimčík
  • 4,548
  • 12
  • 39
  • 58
4
composer dump-autoload
php artisan config:clear
php artisan cache:clear

This will surely work. If not then, you need to do

composer update

and then, the aforementioned commands.

1

You need to assign it to a name space for it to be found.

namespace App\Http\Controllers;
G-Man
  • 1,138
  • 16
  • 20
1

If the issue is not resolved by composer dump-autoload or is not a newly created class.

Check for syntax errors in your class and its traits as well. In my case, there was a syntax error in a Trait, but ReflectionException was shown for the actual Class using that Trait instead.

Manpreet
  • 2,450
  • 16
  • 21
0

When it is looking for the seeder class file, you can run composer dump-autoload. When you run it again and it's looking for the Model, you can reference it on the seeder file itself. Like so,

use App\{Model};
Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20
0

try to use following command

php artisan db:seed --class=DatabaseSeeder
easycodingclub
  • 323
  • 3
  • 5
0

Not strictly related to the question but received the error ReflectionException: Class config does not exist

I had added a new .env variable with spaces in it. Running php artisan config:clear told me that any .env variable with spaces in it should be surrounded by "s.

Did this and my application stated working again, no need for config clear as still in development on Laravel Homestead (5.4)

0

Don't take this as the answer for this question. If it helps others with the same bug but the answers mentioned here not works for them. I also tried all the solutions mentioned here. But my problem was with the namespace I used. The path was wrong.

The namespace I used is:

namespace App\Http\Controllers;

But actually the controller reside inside a folder named 'FrontEnd'

so the solution is change the namespace to:

namespace App\Http\Controllers\Frontend;
Abhi
  • 3,361
  • 2
  • 33
  • 38
0

I've recreated class, and it worked. Copy all file content, delete file, and run php artisan make:seeder ModelTableSeeder. Because composer dump-autoload didn't worked for me.

М.Б.
  • 1,308
  • 17
  • 20
0

You may try to write app in use uppercase, so App. Worked for me.

Epsilon47
  • 768
  • 1
  • 13
  • 28
0

Usually error message ReflectionException: Class app does not exist appears in larval test when you forget to close connection.

if you're using setUp or tearDown function in your test do not forget to close connection by calling parent::setUp and parent::tearDown

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        parent::tearDown();
    }
Suresh Dhakal
  • 21
  • 1
  • 7
0

In case of using psr-0 , check the class name does not contain underscore .

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43
0

I had this error when trying to reach an endpoint in a custom route file, that had a namespace prepended in RouteServiceProvider

  • The namespace in the class was correct.
  • The file path of the class file was correct.
  • The controller call from the routes file was correct (when not taking the prepended namespace from the RouteServiceProvider into account)
  • The default $router->group(['namespace' ...]) in the RouteServiceProvider was incorrect.

Updating the namespace in the RouteServiceProvider solved the issue, as the relative controller path specified in the routes file was now resolving correctly.

xyz
  • 559
  • 2
  • 11
0

Check file/folder permissions

I struggled with this error today, and no amount of cache, config, autoload clears did anything to help. To add to the confusion, the error was thrown if initiated by a web request, but accessing the class in tinker worked fine.

After checking for typo's, syntax errors, and incorrect namespaces, I ended up discovering it was a file permission issue. The folder and file containing my class did not have appropriate permissions so it was throwing this error. The incorrect permission level I had was 771 (folder) and 660 (file), by changing it to 775 and 664 I was able to get it working.

My understanding of the different behaviors is that when running from the command line it was reading the file as my user (which had all the permissions it needed), but when initiated from the web it uses the "other" permission group which could do nothing.

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43
0

For new Laravel users it's a common mistake to forgot to add the name space:

routes/web.php

use App\Http\Controllers\CompanyController;

app/Http/Controllers/CompanyController.php

namespace App\Http\Controllers;

Using Laravel 9.

Zorro
  • 1,085
  • 12
  • 19
-1
composer dump-autoload 

this will fix it

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
-1
composer update 

Above command worked for me.

Whenever you make new migration in la-ravel you need to refresh classmap in composer.json file .

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
divya_kanak
  • 142
  • 9