首页 > 解决方案 > Laravel Blade 中的下拉默认选项错误

问题描述

在我的 laravel 刀片中,我正在尝试显示默认下拉选项,即法语中的 Select role

{!! Form::select('roles', $roles, array_merge(['' => 'Veuillez sélectionner un rôle'], $roles), ['class' => 'form-control txt_txt']) !!}

但这一直给我一个错误,

array_merge(): Expected parameter 2 to be an array, object given 

我正在使用用户角色从数据库中填充该下拉列表,以下是我从控制器中的雄辩

$roles = Role::where('name','<>','Admin')->orderBy('name')->pluck('name', 'name');
        

如何正确显示默认选项?

标签: phplaravel

解决方案


您使用Form::select不正确。它应该是这样的:

Form::select(name, list, selected, selectAttributes, optionsAttributes, optgroupsAttributes);

所以你的代码应该是这样的:

{!!
Form::select(
    'roles',
    array_merge(['' => 'Veuillez sélectionner un rôle'], $roles->toArray())
    '',
    array(
        'class' => 'form-control txt_txt'
    ))
!!}

推荐阅读