Hi There, im on the way of creating multi filter on site
And will have about 6-8 tag fields that will be work with bootstrap multiselect
when im selecting few tags of one field they are sending to controller as array
In controller if I create something like this:
if($categories = get('categories')) {
if(is_array($categories)) {
// fetch children with a date in the past
$reviews = $reviews->filterBy('categories', 'in', $categories);
$categories = implode (", ", $categories);
}
else {
$categories = explode(", ", $categories);
$reviews = $reviews->filterBy('categories', 'in', $categories);
$categories = implode (", ", $categories);
}
}
filter works but it shows only reviews where categories is 100% match (for example if I select category New Reviews, it will shows only reviews where this category only one, if review have two categories New Reviews, Popular Reviews it will not shows in the loop.
implode/explode im using for adding strings to data-categories for ajax loading
If change controller to this
elseif ($categories = get(‘categories’)) {
if(is_array($categories)) {
foreach ($categories as $category) {
$review = $review->listed()->filterBy('categories', '*=', $category)->sortBy('date')->flip();
}
$categories = implode (", ", $categories);
} else {
$categories = explode(", ", $categories);
foreach ($categories as $category) {
$reviews = $reviews->listed()->filterBy('categories', '*=', $category)->sortBy('date')->flip();
}
$categories = implode (", ", $categories);
}
}
It will start filtering reviews with many categories, but if I select two or more categories for filtering controller gives results only for the last selected category…
and if I try to put this all to filter() results are empty…
if(is_array($categories)) {
$reviews = $reviews->filter(function($review) use($categories) {
foreach ($categories as $category) {
$review = $review->listed()->filterBy('categories', '*=', $category)->sortBy('date')->flip();
}
});