DEV Community

Sospeter Mongare
Sospeter Mongare

Posted on

How to run all seeders in Laravel 9

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")
]);
}
Enter fullscreen mode Exit fullscreen mode

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")
]);
}
Enter fullscreen mode Exit fullscreen mode

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
]);
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
nsubramkavin profile image
nsubramkavin

Thank you, this helped my project.