In this article, I will show you how to run all seeders in Laravel 9 instead of running individual seeders.
In order to run all seeders in Laravel, you must first create individual seeders using the artisan command below:
php artisan make:seeder AdminSeeder
The above command creates an Admin seeder and will create a file on Database/seeders with the function below:
public function run()
{
Admin::create([
"name" => "Admin lastname",
"email" => "admin@gmail.com",
"password" => bcrypt("123456")
]);
}
Create another seeder for user as show below:
php artisan make:seeder UserSeeder
When you run, you get the following function:
public function run()
{
User::create([
"name" => "User lastname",
"email" => "user@gmail.com",
"password" => bcrypt("123456")
]);
}
In order to run the specific seeders, you can do this by running the below commad:
php artisan db:seed --class=AdminSeeder
To run all seeders at Onces, you need to register all seeders in the DatabaseSeeder.php file.
public function run()
{
$this->call([
UserSeeder::class
AdminSeeder::class
]);
}
After registering the seeders, you can run using the command below:
php artisan db:seed
Summary
This post has gone a long way to discussing how to run multiple seeders at once rather than one by one. I hope it helped you sort out your issue in case you have several seeders.
Thanks for reading!
Top comments (1)
Thank you, this helped my project.