Monday, May 22, 2017

trying to make a filter with laravel

its strange.. but this code works only once. If i try to re-select - i get an empty table. It seems that re-select is not working properly.

Controller:

  function filter(Request $request){
        if ($request->id == 'all'){
                $goods = Good::with('Shop')->get();
                $shops = Shop::all ();
                return redirect()->back();
        }else{
                $goods = Good::where('shop_id', $request->id)->get();
                $shops = Shop::all ();
                return view('filter')->with(['goods' => $goods, 'shops' => $shops]);
        }
    }

blade file:

<form action = "" method="post">
                                
                                        <select name="id">
                                                @foreach ($shops as $shop)
                                        <option value="" @if (old('shop_id') == $shop->id) selected="selected" @endif> </option>
                                                @endforeach;
                                        <option value="all">All</option>
                                        </select>
                                        <br><br>
                                        <input type="submit" class="btn btn-primary" value="Pasirinkti">
                                </form>

I feel that i need to somehow delete old values from request before re-submitting selection. Or something else :) thanks for your help.



via Benua

Advertisement