Using Artisan::call() to pass non-option arguments

php artisan command list
laravel console log
laravel run artisan command from controller
laravel tinker
laravel commands pdf
artisan::call output
laravel custom artisan command
laravel commands tutorial

In the shell I can create a database migration (for example) like so:

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

Using Artisan::call() I can't work out how to pass a non-argument parameter ("mymigration" in this example). I have tried many variants of the code below:

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

Anyone got any ideas? I have been using shell_exec('./artisan ...') in the meantime but I'm not happy with the approach.

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

Incidentally db:migrate isn't an artisan command out of the box. Are you sure this is correct?

Artisian::call with non-option arguments, How to call artisan commands from controller: Artisian::call with non-option arguments. Posted 4 years But this way don't pass arguments to command. public function fire() { $email = $this->argument('email'); $password  19 Using Artisan::call() to pass non-option arguments Oct 2 '14 3 Connecting to lxc container via SSH using lxc user Dec 3 '15 2 file_get_contents behind a proxy (using proxy list) Jan 25 '12

In laravel 5.1 , you set options with/without values when calling an Artisan command from your PHP code.

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

in this case, inside your artisan command;

$this->option('optionwithvalue'); //returns 'youroptionvalue'

$this->option('optionwithoutvalue'); //returns true

Artisan::call() doesn't accept unnamed arguments · Issue #23805 , Connect with others. Events · Community Artisan::call() doesn't accept unnamed arguments #23805. Closed work anymore. Is there any other way to pass an argument? Optional arguments would be a better solution: email:send {user?} The error is that 'The "namespace" argument does not exist . Alternatively, you may pass the entire Artisan command to the call method as a string: Artisan::call('email:send 1 --queue=default'); Using the queue method on the Artisan facade, you may even queue Artisan commands so they are processed in the background by your queue workers. Before using this method, make sure you have configured your queue

The solution is different if you're using Laravel 5.1 or newer. Now what you need to do is you need to know the name that was given to the argument in the command's signature. You can find the name of the argument from your command shell by using php artisan help followed by the command name.

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

Artisan Console - Laravel, <?php Route::get('example', function() { // Call and Artisan command options, you can do this by passing an array as the second argument to  The first argument (emailClient) is assigned a value. Second argument (disableDataService) is non-option argument. We will access both argument in the application. Each word after the run command is an argument. The arguments that start with '-' are option argument; and others are non-option arguments.

I know this question is pretty old but this came up first on my Google search so I'll add this here. @orrd's answer is correct but I'll also add that for cases that use an array of arguments where you use the asterisk * you need to supply the argument(s) as an array.

For example if you have a command that uses array arguments with a signature like:

protected $signature = 'command:do-something {arg_name*}';

In these cases you need to supply the arguments in an array when you call it.

$this->call('command:do-something', ['arg_name' => ['value']]);
$this->call('command:do-something', ['arg_name' => ['value', 'another-value']]);

Laravel: Calling Artisan Commands From Your Application, Options are not necessarily required, and can be used as a sort of "flag" to Call the command with all parameters, without options. php artisan  Nice comment, but now in 2017, i am trying to find how to pass 'options' to the 'make:model' artisan command within app code, the first argument is easy as it has a clear name of 'name' as follows: From command line: php artisan make:model modelname -mcr (to create migration and a resourced controller) From app: Artisan::call('make:model', [ 'name' => 'modelname', 'options' => '-mcr' ]); ..

In you 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 you command with parameters:

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

For more info refer to this article.

Laravel Artisan Command Input and Command Signatures, You can either call a command using Artisan::call() (which will return the command's command (password:reset); and second, an array of parameters to pass it. arguments are passed by keying to the argument name, and options with no  I have a bash script that employs the read command to read arguments to commands interactively, for example yes/no options. Is there a way to call this script in a non-interactive script passing default option values as arguments? It's not just one option that I have to pass to the interactive script.

Laravel: Up & Running: A Framework for Building Modern PHP Apps, If you're not familiar with Artisan commands, they're command-line functions that you can @return void */ public function __construct() { parent::__construct(); } As you can see, defining arguments and options uses a complicated syntax There are also two new output functions: $this->table() , and the  If you're using GNU make, this is easy to do. The only problem is that make will interpret non-option arguments in the command line as targets. The solution is to turn them into do-nothing targets, so make won't complain:

Creating Artisan commands with the new, simpler syntax in Laravel , You can either call a command using Artisan::call() (which will return the command's command (password:reset); and sec‐ond, an array of parameters to pass it. arguments are passed by keying to the argument name, and options with no  Gotcha. The npm_config variant is much nicer when overriding via the command line (and via npmrc files). The downside is that one can't set defaults in package.json that way. Personally, I find npm_package_config_* settings preferable for use by npm-scripts (since defaults can be set in package.json itself, and overridden via CLI or npmrc files).

Laravel: Up and Running: A Framework for Building Modern PHP Apps, Q&A for Ubuntu users and developers. Stack Exchange Network. Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Comments
  • You are right - I meant migrate:make - but the question was about artisan commands in general. Thanks for your answer.
  • Throws an error in Laravel 5.4. I would recommend Ikos answer.
  • Also this won't work with multiple arguments... Associative arrays must have unique keys....
  • I doesn't work with multiple arguments and from version 5.4
  • I think this answer is solved another problem - How to pass non-value options to artisan command? In my case this was a solution because I needed to pass '--force' option to 'migrate' command.
  • This one does it ! Thank you @iko!
  • From 5.4 this is the only way to do it
  • This is the solution that worked perfectly for myself. Using Laravel 5.3
  • This should be THE solution