dimanche 31 mars 2019

Selecting multiple filters using Query Strings Laravel

Currently I have a filter bar that works, but it only selects one filter at a time, how could I get it to select multiple filters at once by simply adding on to the current query string?

This is what I have in my Controller

class ProductController extends Controller
{
    public function index()
    {

        $products = new Product;

        $queries = [];

        $columns = [
            'beertype_id', 'packaging', 'brewery_id'
        ];

        foreach ($columns as $column){
            if (request()->has($column)){

            $products = $products->where($column, request($column));

                $queries[$column] = request($column);
         }  
        }    
        $products = $products->paginate(10)->appends($queries);
     }
}

In my blade template

<div class="col-md-2 col-centered" id="filter-bar">
     <p>Filter Products:</p>

       <a href="/products">Reset</a>

        <ul>
            <p>Types of Beer:</p>
            <li class="filter-link"><a href="/products?beertype_id=1" >IPA</a></li>
            <li class="filter-link"><a href="/products?beertype_id=2">Lager</a></li>
            <li class="filter-link"><a href="/products?beertype_id=3">Pale Ale</a></li>
            <li class="filter-link"><a href="/products?beertype_id=4">Porter</a></li>
            <li class="filter-link"><a href="/products?beertype_id=5">American Pale Ale</a></li>
            <li class="filter-link"><a href="/products?beertype_id=6">Amber Ale</a></li>
            <li class="filter-link"><a href="/products?beertype_id=7">Rye Ale</a></li>
            <li class="filter-link"><a href="/products?beertype_id=8">Hybrid</a></li>
            <li class="filter-link"><a href="/products?beertype_id=9">Red Ale</a></li>
            <li class="filter-link"><a href="/products?beertype_id=10">Saison</a></li>
            <li class="filter-link"><a href="/products?beertype_id=11">Sour Beer</a></li>
        </ul>


         <ul>
            <p>Packaging:</p>
             <li class="filter-link"><a href="/products?packaging=bottle">Bottle</a></li>
             <li class="filter-link"><a href="/products?packaging=can">Can</a></li>
             <li class="filter-link"><a href="/products?packaging=keg">Keg</a></li>
         </ul>

         <ul>
             <p>Brewery:</p>

             <li class="filter-link"><a href="/products?brewery_id=2">Kinnegar</a></li>
             <li class="filter-link"><a href="/products?brewery_id=3">White Hag</a></li>
             <li class="filter-link"><a href="/products?brewery_id=4">Quilmes</a></li>
             <li class="filter-link"><a href="/products?brewery_id=5"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=6"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=7"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=8"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=9"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=10"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=11"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=12"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=13"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=14"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=15"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=16"></a></li>
             <li class="filter-link"><a href="/products?brewery_id=17"></a></li>
         </ul>

     </div>


Would using the route() helper solve this? if so, how could i go about using it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire