[php] ReflectionException: Class ClassName does not exist - Laravel

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'    => '[email protected]',
        '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.

This question is related to php laravel laravel-5 laravel-artisan

The answer is


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.


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.


composer dump-autoload 

this will fix it


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.


I am not writing as the answer for this question. But I want to help others if they faced the same bug but the answers mentioned here not works. 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;

try to use following command

php artisan db:seed --class=DatabaseSeeder

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)


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.


A composer dump-autoload should fix it.


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};

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


composer update 

Above command worked for me.

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


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.


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'
    ];
}

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.


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

namespace App\Http\Controllers;

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.


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

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


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.


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();
    }

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()

Examples related to laravel-artisan

No Application Encryption Key Has Been Specified Laravel 5.4 create model, controller and migration in single artisan command SQLSTATE[HY000] [2002] Connection refused within Laravel homestead ReflectionException: Class ClassName does not exist - Laravel Artisan, creating tables in database Laravel 5 Clear Views Cache Laravel 5 How to switch from Production mode Could not open input file: artisan Laravel 5 - artisan seed [ReflectionException] Class SongsTableSeeder does not exist What are the Differences Between "php artisan dump-autoload" and "composer dump-autoload"?