【发布时间】:2016-10-03 09:37:56
【问题描述】:
我正在尝试使用 Laravel/Symfony 作为控制台的一部分提供的“选择”功能,但在涉及数字索引时遇到了问题。
我正在尝试模拟 HTML 选择元素的行为,即您显示字符串值但实际上返回的是关联 ID 而不是字符串。
示例 - 不幸的是 $choice 始终是名称,但我想要 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'
]);
}
}
【问题讨论】:
标签: laravel symfony console command