set a default value for select field in Laravel using collective form builder?

旧时模样 提交于 2019-12-22 14:02:07

问题


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')) !!}

回答1:


Third argument is a default for select list, so $roleId should contain default role ID in this case.

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.

Update

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.
?>


来源:https://stackoverflow.com/questions/38811745/set-a-default-value-for-select-field-in-laravel-using-collective-form-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!