我正在尝试使用Laravel/Symfony作为控制台的一部分提供的“选择”功能,并在涉及到数字索引时遇到问题。Laravel选择命令数字键
我试图模拟HTML选择元素的行为,因为您显示字符串值但实际取回关联的ID而不是字符串。
例子 - 不幸的是$选择总是名字,但我想要的ID
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
class DoSomethingCommand extends Command
{
protected $signature = 'company:dosomething';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$choice = $this->choice("Choose person", [
1 => 'Dave',
2 => 'John',
3 => 'Roy'
]);
}
}
解决方法 - 如果我前缀的人ID,然后它工作,但希望能有另一种方法或者这只是一个限制图书馆的?
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
class DoSomethingCommand extends Command
{
protected $signature = 'company:dosomething';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$choice = $this->choice("Choose person", [
"partner-1" => 'Dave',
"partner-2" => 'John',
"partner-3" => 'Roy'
]);
}
}
'$ this'是什么? –