Search code examples
phplaravellaravel-seeding

Passing a quantity or other arguments to a Laravel seeder


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