Select Selected value of a dropdown in laravel 5.4

我们两清 提交于 2019-12-04 09:05:22

Larvel passes the inputs back on validation errors. You can use the old helper function to get the previous values of form. A simple comparison should do the trick.

<div class="form-group {{ $errors->has('designation') ? ' has-error' : '' }}">
    <label for="designation">Designation</label>
    <select id="designation" name="designation" class="form-control">
        <option value="">--- Select designation ---</option>
        @foreach ($designation as $key => $value)
            <option value="{{ $key }}" {{ old('designation') == $key ? 'selected' : ''}}>{{ $value }}</option>
        @endforeach
    </select>
    @if ($errors->has('designation'))
        <span class="help-block">
            <strong>{{ $errors->first('designation') }}</strong>
        </span>
    @endif
</div>

You have to maintain the state of old input by using return redirect() like:

return redirect('form')->withInput();

Retrieving Old Data

To retrieve flashed input from the previous request, use the old method on the Request instance.

$oldDesignation = Request::old('designation');

and check it like:

@foreach ($designation as $key => $value)
  $selected = '';
  @if( $value == $oldDesignation )
    $selected = 'selected="selected"';
  @endif
  <option value="{{ $key }}" {{ $selected }} />{{ $value }}</option>
@endforeach

Reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!