I have this code:
I have changed the code to include ''
on the title value since without the quotes it fails to work
<select class="form-control" name="team" id="team">
<option value="">---------Choose Team---------</option>
@foreach($teams as $team)
<option value="{{$team->id}}" {{(old('team')==$team->id)? 'selected':''}}>{{$team->name}}</option>
@endforeach
</select>
eg.<select name="title">
<option value="1" {{ old('title') == '1' ? 'selected' : '' }}>
Item 1
</option>
<option value="2" {{ old('title') == '2' ? 'selected' : '' }}>
Item 2
</option>
</select>
<select style="width: 100%;" name="id_driver" id="id_driver" >
<option value="" @if (old('id_driver') == "") selected @endif>Select</option>
@foreach(\App\Driver::all() as $driver)
<option value="{{$driver->id}}" @if (old('id_driver') == $driver->id)
selected @endif >(#{{$driver->id}}) {{$driver->business_name}}
</option>
@endforeach
</select>
Laravel 6 or above: just use the old() function for instance @if (old('cat')==$cat->id), it will do the rest of the work for you.
How its works: select tag store the selected option value into its name attribute in bellow case if you select any option it will store into cat. At the first time when page loaded there is nothing inside cat, when user chose a option the value of that selected option is stored into cat so when user were redirected old() function pull the previous value from cat.
{!!Form::open(['action'=>'CategoryController@storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
@foreach ($cats as $cat)
@if (old('cat')==$cat->id)
<option value={{$cat->id}} selected>{{ $cat->title }}</option>
@else
<option value={{$cat->id}} >{{ $cat->title }}</option>
@endif
@endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
<select class="form-control" name="kategori_id">
<option value="">-- PILIH --</option>
@foreach($kategori as $id => $nama)
@if(old('kategori_id', $produk->kategori_id) == $id )
<option value="{{ $id }}" selected>{{ $nama }}</option>
@else
<option value="{{ $id }}">{{ $nama }}</option>
@endif
@endforeach
</select>
this will help you , just compare with old if exist , if not then compare with the default value
<select name="select_name">
@foreach($options as $key => $text)
<option {{ ($key == old('select_name',$default))?'selected':'' }}> {{ $text }} </option>
@endforeach
</select>
the $default
is the value that injected from the controller to the view
The solution is to compare Input::old()
with the $key
variable.
@if (Input::old('title') == $key)
<option value="{{ $key }}" selected>{{ $val }}</option>
@else
<option value="{{ $key }}">{{ $val }}</option>
@endif