Passing a quantity or other arguments to a Laravel seeder

Issue

I would like to pass an argument as to define how many records I want to create during database seeding, without having to edit the factory manually.

I have tried different variations on php artisan db:seed --class=UsersTableSeeder [using different args here]

I can’t seem to find any documentation, so I don’t know if that functionally exists. Does something like that exist?

class UsersTableSeeder extends Seeder
{
    public $limit = null;

    public function __construct($limit = 1) {
        $this->limit = $limit;
    }

    public function run()
    {
      echo $this->limit;
    }
}

Solution

There is no way to directly specify an argument.

If you want to specify a parameter via the command line, you could use an environment variable.

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        $limit = env('SEEDER_LIMIT', 1);

        echo $this->limit;
    }
}

Call like this:

SEEDER_LIMIT=10 php artisan db:seed --class=UsersTableSeeder

Answered By – patricus

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published