从字段数组中的选择字段中预选一个选项


Preselecting an option from a select field in a fields array?

观察下面的$fields数组,它在刀片中用作输入表单:

$fields = [
    'user_id'           => [
        'label' => 'Opportunity Owner' . $req,
        'type'  => 'select',
        'class' => 'select2',
        'opts'  => User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()
    ],
    'name'              => [
        'label' => 'Opportunity Name' . $req,
    ],
    'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
    ],
    'description'       => [
        'label' => 'Description',
        'type'  => 'textarea'
    ],
    [
        'type'  => 'submit',
        'label' => 'Save',
        'class' => 'btn btn-primary !important'
    ]
];

在'agent_id'部分,我想预先选择一个值,如果用户有一个预分配的值。我知道如何从用户获得信息,但我失去了如何在"agent_id"字段内的数组中"选择"一个选项。我需要在选择中显示所有选项,但我希望能够有一个"选择"基于链接到用户的agent_id号码。我尝试了以下操作:

'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
        'selected' => {{appropriate number here}}
    ],

但这不起作用。我该怎么做呢?

添加不带索引的选定值
试试这个:

'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
        {{appropriate number here}}
    ],