Sandeep Bhaskaran Sandeep Bhaskaran - 1 year ago 194
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')) !!}

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download