Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 867
Dmitry Avatar asked Dec 11 '14 15:12

Dmitry


People also ask

How do I seed a specific seeder in Laravel?

Writing SeedersA seeder class only contains one method by default: run . This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.

What is difference between seeder and factory in Laravel?

Database seeder is used to populate tables with data. Model factories is a convenient centralized place to define how your models should be populated with fake data.

Which command is used to specify a specific seeder class to run individually?

Once you're done writing the seeders, use the db:seed command. This will run DatabaseSeeder 's run function. You may also specify to run a specific seeder class to run individually using the --class option.


Video Answer


1 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

like image 187
Eduardo Trujillo Avatar answered Oct 17 '22 19:10

Eduardo Trujillo