Sandeep Bhaskaran Sandeep Bhaskaran - 5 months ago 45
PHP Question

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

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


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.


To add Select Role default value, do this before Form::select clause:

    $rolesList = App\Role::orderBy('name')->lists('label','id');
    $rolesList[0] = 'Select Role';
    ksort($rolesList); // Will resort list.