使用集体表单生成器为Laravel中的select字段设置默认值? [英] set a default value for select field in Laravel using collective form builder?
问题描述
我的表单中有一个用于选择管理员角色的选择字段.我需要为该选择字段设置一个默认值,例如选择角色".我正在使用Laravel 5.2和集合表单生成器类.这是我的代码>
I have a select field in my form for selecting admin roles.I need to set a default value for that select field like 'Select Role'.I am using Laravel 5.2 and collective form builder class.here is my code
{!! Form::select('role_id',App\Role::orderBy('name')->lists('label','id'),$roleId,array('class'=>'form-control col-md-7 col-xs-12','id'=>'role_id')) !!}
推荐答案
选择列表的默认参数为第三,因此在这种情况下,$roleId
应包含默认角色ID.
Third argument is a default for select list, so $roleId
should contain default role ID in this case.
如果它不起作用,则应检查$roleId
包含的内容,并查看由Form::select
子句生成的HTML,以查找问题.
If it doesn't work, you should check what $roleId
contains and also look into HTML generated by Form::select
clause to find a problem.
更新
要添加Select Role
默认值,请在Form::select
子句之前执行此操作:
To add Select Role
default value, do this before Form::select
clause:
<?php
$rolesList = App\Role::orderBy('name')->lists('label','id');
$rolesList[0] = 'Select Role';
ksort($rolesList); // Will resort list.
?>
这篇关于使用集体表单生成器为Laravel中的select字段设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!