Custom Filters
- Posted April 24, 2017
- Developer Tools
Related Links
Introduction
This article will show you how to define custom filters for columns not based on streams fields or in the case that you need more complicated querying.
Filter Types
There are two primary filter types by default aside from the automated search
and field
filter types:
Input Filter
The input
filter type will display a generic text input for the filter input. An example of a custom input
type filter might look like this:
'name' => [
'filter' => 'input',
'query' => MyCustomQuery::class,
'placeholder' => 'Custom Filter',
],
Select Filter
The select
filter type will display a dropdown select input for the filter input. An example of a custom select
type filter might look like this:
'status' => [
'filter' => 'select',
'query' => MyCustomFilter::class,
'options' => [
'live' => 'Live',
'draft' => 'Draft',
'scheduled' => 'Scheduled',
],
],
The Query Handler
The query
parameter must be a resolvable value like a closure or class/handler string. Just like other handlers, if no @method
is defined then @handle
will be assumed. There are 3 parameters available for injection:
\Illuminate\Database\Eloquent\Builder $query
\Anomaly\Streams\Platform\Ui\Table\TableBuilder $builder
\Anomaly\Streams\Platform\Ui\Table\Component\Filter\Contract\FilterInterface $filter
You can also inject anything else you need as this class is resolved from and called using Laravel's service container.
A simple query might look like this:
public function handle(Builder $query, FilterInterface $filter)
{
$query->where($filter->getSlug(), 'LIKE', "%{$filter->getValue()}%");
}