Using Artisan :: call () to pass optional arguments

In the shell, I can create a database migration (for example) as follows:

./artisan migrate:make --table="mytable" mymigration 

Using Artisan :: call () I cannot decide how to pass the parameter without an argument ("mymigration" in this example). I have tried many code options below:

 Artisan::call('db:migrate', ['--table' => 'mytable', 'mymigration']) 

Does anyone have any ideas? However, I am using shell_exec ('./artisan ...'), but I am not happy with this approach.

+6
source share
4 answers

Artisan::call('db:migrate', ['' => 'mymigration', '--table' => 'mytable']) should work.

By the way, db: migrate is not a craft team out of the box. Are you sure this is correct?

+6
source

The solution is different if you are using Laravel 5.1 or later. Now you need to know the name that was passed to the argument in the command signature. You can find the argument name from the shell using php artisan help followed by the command name.

I think you wanted to ask about "make: migration". So, for example, php artisan help make:migration shows you that it takes a parameter named "name". So you can call it this way: Artisan::call('make:migration', ['name' => 'foo' ]) .

+5
source

In laravel 5.1, you set parameters with / without values ​​when you call the Artisan command from your PHP code.

 Artisan::call('your:commandname', ['--optionwithvalue' => 'youroptionvalue', '--optionwithoutvalue' => true]); 

in this case, inside your artisan team;

 $this->option('optionwithvalue'); //returns 'youroptionvalue' $this->option('optionwithoutvalue'); //returns true 
+3
source

In the command, you add getArguments ():

 /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('fdmprinterpath', InputArgument::REQUIRED, 'Basic slice config path'), array('configpath', InputArgument::REQUIRED, 'User slice config path'), array('gcodepath', InputArgument::REQUIRED, 'Path for the generated gcode'), array('tempstlpath', InputArgument::REQUIRED, 'Path for the model that will be sliced'), array('uid', InputArgument::REQUIRED, 'User id'), ); } 

You can use the arguments:

 $fdmprinterpath = $this->argument('fdmprinterpath'); $configpath = $this->argument('configpath'); $gcodepath = $this->argument('gcodepath'); $tempstlpath = $this->argument('tempstlpath'); $uid = $this->argument('uid'); 

call the command with parameters:

 Artisan::call('command:slice-model', ['fdmprinterpath' => $fdmprinterpath, 'configpath' => $configpath, 'gcodepath' => $gcodepath, 'tempstlpath' => $tempstlpath]); 

See the article for more information.

0
source

Source: https://habr.com/ru/post/976140/


All Articles