1

I have a form based on selects for a search with filters (picture attached below). When i pressed the search button, the page reloads and it reset all the selects.

I try to use the old helper, but it does not work properly, it just takes the first option of the select( and it gets rid of the default option as you can see in the pictures below)

Things to know: it is a form with a get action, it is not intended to store or edit something, only for search. So, how can i properly get the old data when refresh or after pressing the search button?

Example1 example2 examplegui

This is my first attempt and it did not work same for this one too. as you can see in this image, it deletes the default option, and it shows the first option

Blast06
  • 791
  • 1
  • 10
  • 16

3 Answers3

1

The old() function will get data by specific key from the flash data. That means you must set the data as a flash before using the old().

Source: https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Http/Concerns/InteractsWithFlashData.php

Documentation: https://laravel.com/docs/5.8/requests#old-input

In the case, if you want to refresh and keep the input, I suggest using the front-end handling way: When you change the input of drop-down list, push the new query string param into the URL by javascript (How can I add or update a query string parameter?) . Until the user tries to refresh the page, the URL was kept your old value. In the blade form, you can use the function to get the param from GET request with a priority higher than the old() function, I don't actually remember the function to get param from URL, but it will seem like:

{{ Request::get('yourInput') ?? old('yourInput') ?? $realEntity->yourInput }}

Vuong
  • 617
  • 11
  • 26
0

Use the following in your blade files. You can check if the name exists in the url (query string). Hope this helps :)

{{ Request::get('name') }}
0

I think, that old() function should work just fine, but you should compare the value of option in foreach.

<select name="tipo_propiedad">
@foreach ($tipos_propiedades as $tipo_propiedad)
  <option value="{{$tipo_propiedad->id}}" @if($tipo_propiedad->id == old('tipo_propiedad') selected @endif(>{{$tipo_propiedad->name}}</option>
@endforeach
</select>
James
  • 214
  • 1
  • 8