简体   繁体   中英

How to keep value of selected option after form is submitted in Laravel form

I want to make when user search by country on the page refresh eg. result page to keep selected country on the option on the form.

This is what I have where I populate the dropdown

@foreach ($countries as $country)
        <option value="{!! $country->id !!}">{!! $country->name !!}</option>
@endforeach

And this is what I have tried but seems that is not correct since the value isn't selected

@foreach ($countries as $country)
    <option value="{!! $country->id !!}"  @if( old('country->id')  == $country->id) selected="selected" @endif>{!! $country->name !!}</option>
@endforeach

What's the trick here?

Here is the controller

public function search(Request $request)
{

    $searchText = strip_tags($request['q']);
    $seachLocation = strip_tags($request['l']);


    $columns =['alias','description','address1','address2','address3'];

    $query = Item::select('*');

    $query->where( 'title', 'like', '%'.$searchText.'%');

    foreach( $columns as $column) {
        $query->orWhere( $column, 'like', '%'.$searchText.'%', '%'.$seachLocation.'%');
    }

    $query->orWhereHas('category',function( $query ) use (  $searchText ) {
        $query->where('title', 'like', '%'.$searchText.'%' );
    });

    $query->orWhereHas('country',function( $query ) use (  $searchText ) {
        $query->where('name', 'like', '%'.$searchText.'%' );
    });

    $items = $query->paginate(5);
    $searchQuery = $searchText;
    $searchQueryLoc = $seachLocation;

    return view('search', compact('items','searchQuery','seachLocation'));
}

To use the old() helper, your form inputs need to have a name.

You then use the name of the input as the parameter in the old() helper to get the old value.

So using it like old('country->id') won't work; you need to use old('nameAttributeOfInput') .

A full example assuming the name attribute of this input is country_id :

@foreach ($countries as $country)
    <option value="{!! $country->id !!}"  @if( old('country_id')  == $country->id) selected="selected" @endif>{!! $country->name !!}</option>
@endforeach

As a side note, just be aware that {!! !!} {!! !!} does not escape the value being placed on the page which can be potentially dangerous. You would probably be just as fine, and safer, to use {{ }} which escapes the value.

I would suggest that you use session to store the selected country. For example:

session()->put('forms.country', $request->get('country'));

Then your html should become:

<option value="{{ $country->id }}"  @if( session('forms.country')  == $country->id) selected="selected" @endif>{{ $country->name }}</option>

您可以在表单的字段中使用Laravel内置的old()来保存所提供的值。

 @if( old('field_name') == $country->id) selected="selected" @endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM