Good afternoon, I am writing a smart filter for the site by analogy with Yandex.Market. Actually that day, the implementation of the relative filter was puzzled.
What we have:
Dynamic form: (The form is generated automatically, but for clarity, inserted HTML)
<form method="get" action=""> <div> <label>Asus</label> <input type="checkbox" name="filter[]" value="Asus"> <label>Acer</label> <input type="checkbox" name="filter[]" value="Acer"> <label>Lenovo</label> <input type="checkbox" name="filter[]" value="Lenovo"> </div> <div> <label>2015</label> <input type="checkbox" name="filter[]" value="2015"> <label>2014</label> <input type="checkbox" name="filter[]" value="2014"> <label>2013</label> <input type="checkbox" name="filter[]" value="2013"> </div> <button>Искать</button> </form> After submitting the form there is a url of the following form:
site.com?filter=Asus&filter=2015 We also have tables in the database:
products (id, title) products_to_filter (id, product_id, filter_id) The handler itself looks like this (PS Laravel framework)
Выборка соответствующих фильтров. $filter[] = explode(',', $request->input('filter'); $filters = DB::table('products_to_filter')->whereIn('id', $filter) ->get() ->all(); foreach($filters as $row) {$filterArray[] = $row->product_id;} Выборка продуктов: $products = DB::table('products')->whereIn('id', $filterArray)->get()->all(); Actually, my problem is the following, the filter works, but not quite as I need, tobish when filtering, it should reduce the results, but on the contrary it searches for more depth, and as a result increases the number of results. How to solve this problem?