1
私は検索フォームを持っていますが、以前に選択したオプションは表示されません。ここsubmitフォームの後に古い値が表示されないlaravel 5.3
<form role="form" class="advanced-search-form" method="post" action="{{ url('/searchresults') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-4 form-group">
<label for="exampleSelect1">Select city: </label>
<select class="form-control" id="exampleSelect1" name="selectcity">
@if (count($cities) > 0)
@foreach ($cities as $city)
<option value="{{$city->id}}" @if(old('selectcity') == $city->id) selected="selected" @endif>{{$city->name}}</option>
@endforeach
@endif
</select>
</div>
<div class="row">
<div class="form-group col-md-4 col-md-offset-8">
<button class="btn btn-light-blue-2 pull-right" type="submit">Search</button>
</div>
</div>
</form>
は私のルートである:ここで
は私のフォームです
Route::post('/searchresults', '[email protected]');
私のコントローラ内のアクション
public function index()
{
$cities = DB::table('cities')
->select('cities.name', 'cities.id')
->orderBy('name', 'ASC')
->get();
return view('pages.searchresults', compact('cities'));
}
私は結果を表示していない瞬間、なぜフォームが送信後に空になるのかを修正する必要があり、古い値を返さず選択されたオプションを設定します。
のようなリダイレクトを使用する必要があります。 –
第1オフ:フォームを操作して、フォームに入力する場合は、https://laravelcollective.com/docs/5.0/htmlを参照する必要があります。 第2に、未加工のクエリを実行する代わりに都市用のモデルを使用していない理由がありますか? 第3に、フォームにリダイレクトしてデータを保持するには、redirect() - > back() - > withData()を使用し、 - > with(compact( 'cities')) – larsemil
['old()'] (https://laravel.com/docs/5.4/helpers#method-old)は、ドキュメントに記載されているように、古い入力データをセッションにフラッシュする場合にのみ機能します。このデータを覚えたければ、手動でフラッシュするか、選択した値をページに送り返す必要があります。 – Jerodev