Home » Php » php – Laravel 5: How can I add seeder class to autoload?

php – Laravel 5: How can I add seeder class to autoload?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I follow the docs: http://laravel.com/docs/master/migrations#database-seeding

I placed UserTableSeeder file near DatabaseSeeder. In resources/database/seeds/ folder.
These files are without namespaces (only classes in app/ are namespaced).

Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'

What is the best way to solve this problem?

How to&Answers:

The default Laravel 5 project has a classmap defined in its composer.json:

{
    // ...
    "autoload": {
        "classmap": [
            "database"
        ],
        // ...
    }
}

Run composer dump every time you add or remove a class on your database directory to update the Composer autoloader


Reference: https://github.com/laravel/laravel/blob/develop/composer.json

Answer:

You should use composer dump-autoload command. From the docs:

Once you have written your seeder, you may need to regenerate Composer’s autoloader using the dump-autoload command:

composer dump-autoload

Reference here.