Logo Beartropy Tables

Beartropy Tables

Filters
Filtering data in the table.

Defining Filters

[Filter]::make(string label, ?string $column)
In addition to the search input that filters globally on the table, you can add filters by column.
Note: if you pass only the label parameter, the filter will search for a column with same label and match, if you want to specify a column use the second parameter to assign the filter to the column label you want.

To enable filters, you need to create a "filters" function that returns an array of any of the Filter objects we provide.

1public function filters(): array {
2 return [
3 FilterString::make('name'),
4 FilterDateRange::make('Created At')
5 ];
6}

String filter

FilterString::make(string $label, ?string $column)
This filter will search in column with label $label and filter the table accordingly.

1FilterString::make('name') # This will search in column with label 'name'
2 
3FilterString::make('Edad', 'age') # This will search in column with label 'age'

DateRange Filter

FilterDateRange::make(string $label, ?string $column)
This filter uses flatpickr to display a date range select and filter the table based on user input.

1FilterDateRange::make('created_at') # This will search in column with label 'name'

Select Filter

FilterSelect::make(string $label, array $options)
This filter displays a select with the options given.

1FilterSelect::make('type',["primary","secondary"])

Select Magic Filter

FilterSelectMagic::make(string $label, array $options)
Same as select filter, but with magic. This filter with get all values from the column given, make the unique and display them.

1[
2 ["id"=>1, "color"=>"red", "type"=>"Primary"],
3 ["id"=>2, "color"=>"violet", "type"=>"Secondary"],
4 ["id"=>2, "color"=>"green", "type"=>"Primary"],
5]
6 
7FilterSelectMagic::make('type')
8# Will display Primary and Secondary in the <select>

Bool Filter

FilterBool::make(string $label, ?string $column)
Adds a filter for boolean columns (true/false).

1FilterBool::make('isprimary')

Custom Filter Logic

->query(callable $callback)
For complex scenarios where standard filtering isn't enough (e.g., filtering by a value in a HasMany relation), you can define a custom query callback.

1FilterString::make('Theme', 'theme_setting')->query(function($query, $value, $filter) {
2 // $value is the user input
3 $query->whereHas('settings', function($q) use ($value) {
4 $q->where('key', 'theme')->where('value', 'like', "%$value%");
5 });
6});