2014-10-02 60 views
6

在shell我可以创建一个数据库迁移(例如),像这样:使用Artisan ::调用()通过非选项参数

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

使用Artisan ::调用()我不能弄清楚如何传递非参数参数(本例中为“mymigration”)。我已经尝试了以下代码的很多变体:

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

任何人有任何想法?在此期间,我一直在使用shell_exec('./ artisan ...'),但我对这种方法并不满意。

回答

6

Artisan::call('db:migrate', ['' => 'mymigration', '--table' => 'mytable'])应该工作。

顺便说一句db:迁移不是开箱即用的工匠命令。你确定这是正确的吗?

+0

你是对的 - 我的意思是迁移:make - 但问题是关于一般的工匠命令。感谢您的回答。 – GuruBob 2014-10-02 11:18:02

5

如果您使用Laravel 5.1或更新版本,解决方案会有所不同。现在你需要做的是你需要知道命令签名中参数的名称。您可以通过使用php artisan help后跟命令名称从命令shell中找到参数的名称。

我想你的意思是询问“make:migration”。因此,例如php artisan help make:migration显示它接受一个名为“name”的参数。所以你可以这样称呼它:Artisan::call('make:migration', ['name' => 'foo' ])

+1

这也是Laravel 4.2的正确方法 – 2016-02-04 10:50:58

+0

这是为我自己完美工作的解决方案。使用Laravel 5.3 – dxhans5 2017-01-18 21:27:21

3

在laravel 5.1中,当您从PHP代码调用Artisan命令时,可以使用/不使用值来设置选项。

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

在这种情况下,在您的工匠命令;

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

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

我认为这个答案解决了另一个问题 - 如何将非价值选项传递给artisan命令?在我的情况下,这是一个解决方案,因为我需要将'--force'选项传递给'migrate'命令。 – MingalevME 2017-03-01 05:35:22

0

在你指挥你添加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'), 
    ); 
} 

您可以使用参数:

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

叫你命令参数:

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

对于更多信息请参考此article