Setting selected option in laravel form
You can do it like this.
<select class="form-control" name="resoureceName">
<option>Select Item</option>
@foreach ($items as $item)
<option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
@endforeach </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.