Laravel Command 传入参数为 json 格式的问题
在 Laravel
的 Command
中想要传入 Json 格式的数据时,需要将参数作为 Option
传入,在 Command 中 Option 参数与 Argument 参数有一些不同。
如果用 argument 的方法则无法将 json 格式的数据作为参数传入,因为 Command 会将 Json
格式中 { 'a':1, 'b':2 }
作为两个参数 'a':1
, 'b':2
来处理。
Tip:argument
与 option
的不同,argument
需要按照先后顺序传入参数,option
可以无序,但是需要将参数名写清楚,不然 command 会以 argument
的形式传参
// 在这里 data 用 json 格式作为 option 方法传入
protected $signature = "test:create
{--id= : ID}
{--user= : 用户id}
{--page=index : 跳转页面}
{--data= : 参数,使用json格式传入}";
// 执行命令
php artisan test:create
// 打印结果
array(12) {
'id' =>
string(43) "3vWsr1w9FuU94NDOBiZA4iiSmtV-YXaLv6HR7sCHihY"
'user' =>
string(28) "oY2nd4rfF3b1zWwG6FcFnzLL0PJw"
'page' =>
string(11) "index/index"
'data' =>
string(173) "{"phone_number2":{"value":"158********"},"name1":{"value":"吴"},"phrase5":{"value":"配送中"},"thing4":{"value":"网络"},"time3":{"value":"2020-04-01 12:34:56"}"
'help' =>
bool(false)
'quiet' =>
bool(false)
'verbose' =>
bool(false)
'version' =>
bool(false)
'ansi' =>
bool(false)
'no-ansi' =>
bool(false)
'no-interaction' =>
bool(false)
'env' =>
NULL
}