HartleySan's avatar

Is it possible to pass arguments to the php artisan migrate command?

For local dev of a Laravel project I'm working on, I often run the following Artisan command to refresh my local DB for testing/dev purposes:

php artisan migrate:fresh --seed

However, there are times when it would be really handy if I could pass one or more additional arbitrary arguments to the migrate command so that I could affect how the seeders function.

For example, I would like to be able to do run a command like the following:

php artisan migrate:fresh --seed --no-extra-users

And hypothetically, the --no-extra-users argument/option would be somehow made available in the database/seeders/DatabaseSeeder.php file so that I could check to see whether it's set or not, and then change up the logic in the DatabaseSeeder file accordingly.

Is this possible? I've been Googling the heck out of this, but have yet to see anything that suggests it's possible. Thank you.

0 likes
7 replies
bugsysha's avatar

For every command you can run --help and see if it does accept any arguments. You can use php artisan db:seed --class=NameOfTheSeederClass to run specific seeder. So the point is just split seeders or reuse logic from one and that is it.

neilstee's avatar
neilstee
Best Answer
Level 34

@hartleysan looks like it's not available in the current php artisan migrate:fresh --seed command.

Although what you can do is create one for yourself: https://laravel.com/docs/8.x/artisan#writing-commands

Then you can call commands from your newly created command using this: https://laravel.com/docs/8.x/artisan#calling-commands-from-other-commands

Then you can do whatever you want in the handle() function based on the pass arguments.

HartleySan's avatar

Thanks a lot, NEILSTEE. You literally answered my question while I was typing a reply to bugsysha. I think this solves my problem. Thanks again!

1 like
HartleySan's avatar

Thanks a lot for the quick reply, bugsysha.

In my case, I have about 20-30 seeders running, and with the hypothetical options that I want to add to the command, I just want to turn off one or two seeders here or there.

Is there any easy way to do that without having to specify 30-some commands to run just the ones I want? I was thinking about maybe writing a custom command to run all the seeders I want to run, but wasn't sure of the best way to do that. Any advice? Thanks.

bugsysha's avatar

@hartleysan yes it is possible, but you've accepted a solution so I guess there is no point it writing anything now 🤣

HartleySan's avatar

Sorry, bugsysha. Greatly appreciate your help. I think your answer is great, but I would just prefer to wrap everything in an easy-to-run command in this case.

Please or to participate in this conversation.