I need to give selected value like this html:
You can also try this for limited options:
<select class="form-control required" id="assignedRole">
<option id = "employeeRole" selected ="@if($employee->employee_role=='Employee'){'selected'}else{''} @endif">Employee</option>
<option id = "adminRole" selected ="@if($employee->employee_role=='Admin'){'selected'}else{''} @endif">Admin</option>
<option id = "employerRole" selected ="@if($employee->employee_role=='Employer'){'selected'}else{''} @endif">Employer</option>
</select>
Everybody talking about you go using {!! Form::select() !!}
but, if all you need is to use plain simple HTML.. here is another way to do it.
<select name="myselect">
@foreach ($options as $key => $value)
<option value="{{ $key }}"
@if ($key == old('myselect', $model->option))
selected="selected"
@endif
>{{ $value }}</option>
@endforeach
</select>
the old()
function is useful when you submit the form and the validation fails. So that, old()
returns the previously selected value.
You have to set the default option by passing a third argument.
{{ Form::select('myselect', [1, 2], 2, ['id' => 'myselect']) }}
You can read the documentation here.
Another ordinary simple way this is good if there are few options in select box
<select name="job_status">
<option {{old('job_status',$profile->job_status)=="unemployed"? 'selected':''}} value="unemployed">Unemployed</option>
<option {{old('job_status',$profile->job_status)=="employed"? 'selected':''}} value="employed">Employed</option>
</select>
Just Simply paste this code you will get the desired output that you needed.
{{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `
If you have an Eloquent Relationship between your models you can do something like that:
@foreach ($ships as $ship)
<select name="data[]" class="form-control" multiple>
@foreach ($goods_containers as $container)
<option value="{{ $container->id }}"
@if ($ship->containers->contains('container_id',$container->id ) ))
selected="selected"
@endif
>{{ $container->number}}</option>
@endforeach
</select>
@endforeach