Legliez
Legliez

Reputation: 5

Laravel search-filter

I have a problem with passing the selected index to my controller via click. If I manually change the index in the browser, it is working. (http://localhost:3000/admin/users?user=&sortBy=5) $sortOptions is the name of my 2d array in my controller. sortDisplay is a field in my 2d array in my controller.

Am I missing something in my foreach loop?

<label for="sortBy">Sort by</label>
            <select class="form-control" name="sortBy" id="sortBy">
                @foreach($sortOptions as $index => $sortOptions)
                    <option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
                        {{$sortOptions["sortDisplay"]}}
                    </option>
                @endforeach
            </select>

Upvotes: 0

Views: 53

Answers (2)

Stefano
Stefano

Reputation: 178

In the foreach loop, you are assigning the same variable name as the variable you are iterating. In your case, after the first loop, you re-instantiate the $sortOptions variable with the content of the first index of $sortOptions.

@foreach($sortOptions as $index => $sortOption) // <-- $sortOption, not $sortOption(s)
    <option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
        {{ $sortOption["sortDisplay"] }}
    </option>
@endforeach

Upvotes: 0

Abdul Moiz
Abdul Moiz

Reputation: 492

Use jQuery to submit form when value changed So your page will refresh and you will get what you want

Upvotes: 1

Related Questions