I need to give selected value like this html:
<select name="myselect" id="myselect">
<option value="1">Item 1</option>
<option value="2" selected='selected'>Item 2</option>
how can I achieve this, with laravel forms?
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 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With