我正在嘗試使用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'是什麼? –