I have problem with seeders in laravel 9. The problem is when I want to execute all seeders with commands like php artisan db:seed, php artisan db:fresh and similar commands that should execute all seeders. Seeder are only working when I specify exact class with command like php artisan db:seed --class=UserSeeder and similar command that are executing specific seeder. How I can fix this problem and execute all seeders?
CodePudding user response:
The php artisan db:seed
command's --class
flag has a default of Database\Seeders\DatabaseSeeder
, which you can see by issuing php artisan db:seed --help
:
Description:
Seed the database with records
Usage:
db:seed [options] [--] [<class>]
Arguments:
class The class name of the root seeder
Options:
--class[=CLASS] The class name of the root seeder [default: "Database\Seeders\DatabaseSeeder"]
--database[=DATABASE] The database connection to seed
--force Force the operation to run when in production
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
If you'd like to run other seeders without having to manually specify the class via --class
, you can do so within DatabaseSeeder
via $this->call(...)
.
CodePudding user response:
You need to add all seeders that you need to run to your DatabaseSeeder.php file. Then run php artisan db:seed
public function run()
{
$this->call([
UserSeeder::class,
PostSeeder::class,
CommentSeeder::class,
]);
}
https://laravel.com/docs/9.x/seeding#calling-additional-seeders