Passing Arguments to Laravel Seeders: A Reality Check
You want to control the volume of data your database seeding process generates without touching the factory definitions. Specifically, you are looking for a way to pass an argument to your seeder via the command line, perhaps intending to run php artisan db:seed --class=UsersTableSeeder 100 to generate exactly one hundred records.
While the intuition behind this approach is soundâdynamic configuration is a core part of developer workflowsâthe answer is no, Laravel does not support passing arguments to seeders via command-line flags in the way you might expect with other artisan commands.
The php artisan db:seed command accepts a specific set of parameters: --class to specify which seeder to run and --force to override existing data. There is no built-in mechanism to inject additional arguments directly into the constructor or logic of the seeder class during execution. If you attempt to pass extra values after the class name, Artisan will simply ignore them or treat them as an error, depending on your Laravel version.
To achieve the goal of dynamic record counts, you must architect the solution within the codebase itself rather than relying on command-line flag injection. The most robust pattern involves passing a value to the run method explicitly, but since Artisan doesn't provide a native hook for this, you have to rely on environment configuration or file-based state.
Environment Configuration
The standard industry practice for injecting values into long-running processes like seeding is to utilize environment variables. You can define a variable such as DB_SEED_LIMIT in your .env file. Inside your seeder's run() method, you check if this variable exists and use it to calculate your batch size or total limit.
class UsersTableSeeder extends Seeder
{
public function run()
{
$limit = config('app.env') === 'local'
? env('DB_SEED_LIMIT', 100) // Default to 100 if not set
: 5; // Production defaults to a smaller seed set
$factory = Factory::model(User::class);
// Generate the desired quantity
foreach (range(1, $limit) as $index) {
$user = $factory->create([
'name' => Faker()->name(),
'email' => Faker()->uniqueEmail(),
]);
// Logic to handle unique IDs or relationships goes here
}
}
}
This approach keeps your command line clean (php artisan db:seed --class=UsersTableSeeder) while allowing you to toggle behavior via .env. It aligns with Laravel's philosophy of keeping configuration separate from logic.
The Trade-Off of Hard-Coding Limits
Some developers attempt the pattern shown in your source material, defining a $limit property in the constructor. While syntactically valid PHP, this does not solve the specific problem of passing arguments via the command line without modifying the code. To make that constructor-based approach work with CLI arguments, you would need to write a custom Artisan command or use an alias that dynamically instantiates the class with those arguments.
For example, creating a custom command like php artisan seed-users:100 is possible but adds significant boilerplate compared to simply setting an environment variable. The custom command would look like this:
class SeedUsersCommand extends Command
{
protected $signature = 'seed:users {--limit : The number of records}';
protected $description = 'Seed the users table with a specific quantity';
public function handle()
{
$limit = $this->option('limit');
// Instantiate the seeder manually to pass the argument
$seeder = new UsersTableSeeder($limit);
$seeder->run();
return 0;
}
}
However, this requires registering the command in routes/console.php or the Kernel.php. It is a valid solution but introduces complexity that environment variables avoid. If your team prefers not to pollute the .env file with specific seed counts, the custom command route is the architectural alternative.
Conclusion
Laravel provides strict boundaries around how seeders interact with the CLI. Direct argument passing is not supported natively. To pass quantities or other arguments effectively, you must choose between environment variables for flexibility or custom commands for explicit control. Both methods allow you to avoid editing factories manually, adhering to the principle of keeping your data generation logic decoupled from static factory definitions.