I have a form that uses various filters to query a set of pages. This is all done via a controller (structurePluck
is a custom method):
<?php
return function ($page) {
//get Used Filters
$query = get('q');
$topPlace = get('topPlace');
$type = get('type');
$ucategories = get('categories');
$utags = get('tags');
//Get All Places for Filter Values
$places = $page->children()->listed()->flip()
->when($type, function ($type) {
if($type != 'all'){
$return = $this->filterBy('type', $type);
} else {
$return = $this;
}
return $return;
});
//Get All Filter Values
$categories = $places->pluck('category',',',true);
$tags = $places->pluck('tags',',',true);
$districts = $places->structurePluck('locations', 'district');
$kiez = $places->structurePluck('locations', 'kiez');
//Apply Used Filters
$aplaces = $page->children()->listed()->flip()
->when($query, function ($query) {
return $this->search($query, 'title|category|tags|locations');
})
->when($topPlace, function ($topPlace) {
return $this->filterBy('topPlace', true);
})
->when($type, function ($type) {
if($type != 'all'){
$return = $this->filterBy('type', $type);
} else {
$return = $this;
}
return $return;
})
->when($ucategories, function ($ucategories) {
return $this->filterBy('category', 'in', $ucategories, ',');
})
->when($utags, function ($utags) {
return $this->filterBy('tags', 'in', $utags, ',');
});
//Get Available Filter Values
$acategories = $aplaces->pluck('category',',',true);
$atags = $aplaces->pluck('tags',',',true);
$adistricts = $aplaces->structurePluck('locations', 'district');
$akiez = $aplaces->structurePluck('locations', 'kiez');
return [
//results
'aplaces' => $aplaces,
//All Filter Values
'categories' => $categories,
'tags' => $tags,
'districts' => $districts,
'kiez' => $kiez,
//Used Filters
'query' => $query,
'topPlace' => $topPlace,
'type' => $type,
'ucategories' => $ucategories,
'utags' => $utags,
//Available Filters
'acategories' => $acategories,
'atags' => $atags,
'adistricts' => $adistricts,
'akiez' => $akiez,
];
};
‘All Filter Values’ are exactly that: all the available filter options. ‘Used Filters’ are filters that have been set in the frontend and passed via URL parameters. ‘Available Filters’ are all the possible filters plucked from the filtered results (if that makes sense).
The form works great and I am able to show/hide, enabled/disable relevant fields based on what the user has already selected. However, ideally I’d like to have this function as a ‘live search’. I’ve crudely mocked up this behaviour by refreshing the page with JS on a form change
event. Is it possible to reuse any of the existing logic (with the addition of routes), or would I need to rebuild the whole thing using a JSON content representation and deal with all the filtering via JS?