How to call Seeder Class Laravel

Laravel allows you to seed your database with data by using a seed class. The seed classes will come under the database/seeders directory. The DatabaseSeeder class is defined by default. In this class, you can use the call method to run other seed classes, this allows you to control the seeding order. Let’s learn how to call Seeder class in Laravel

Call Seeder Class

You can use the call method inside the DatabaseSeeder class to execute any other seed classes. The call method will allow you to split up your database and seed it into multiple files so there is not a single very huge seeder class. This makes it suitable to handle large data records and applicable in many cases like import large data records. The call method will take in an array of seeder classes that will be executed. Consider the code sample shown below.

/**
 * Run the database seeders.
 *
 * @return void
 */
public function run()
{
    $this->call([
        CustSeeder::class,
        BookingSeeder::class,
        PaymentsSeeder::class,
    ]);
}

Run using Artisan

To execute the call method above in your seeder class, you should run the seeder class.

The db:seed php Artisan command will help seed your database. The command will run the Database\Seeders\DatabaseSeeder class by default. Thereby executing the run method in it and segregating the defined seeder classes to database files.

php artisan db:seed

If you want to run a seed class separately, you can use the --class option as shown below and specify the seeder class you need to run separately.

php artisan db:seed --class=CustSeeder