A Laravel Nova column queryer filter
A Laravel Nova column querier filter for Nova > v1.1.8
Prior to Nova v.1.3.2, use the 0.2.1 version.
Prior to Nova v.1.1.8, use the 0.1.1 version.
Run this command in your Laravel Nova project:
composer require philperusse/nova-column-filter
Create a filter with artisan
$ php artisan nova:filter UserColumnFilter
Extend your filter class with the ColumnFilter class instead of Nova’s base Filter class and add the columns on which you want to filter on in the options
use \philperusse\Filters\ColumnFilter as Filter;
class ColumnFilter extends Filter
{
/**
* Apply the filter to the given query.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function apply( Request $request, $query, $value )
{
$args = collect($value)->values()->filter(); //Remove any empty keys.
if($args->isEmpty())
return $query;
return $query->where(...$args->all());
}
/**
* Get the filter's available options.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function options( Request $request ) : array
{
return array_merge(parent::options($request), [
'columns' => [
'name' => 'Name',
'age' => 'Age',
]
]);
}
}
You can customize the operator list by overriding the operators
key in the filters options.
use philperusse\Filters\ColumnFilter as Filter;
class ColumnFilter extends Filter
{
public function options( Request $request )
{
return array_merge(parent::options($request), [
'columns' => [
'name' => 'Name',
'age' => 'Age',
],
'operators' => [
'=' => '=',
'>' => '>',
'>=' => '≥',
'<' => '<',
'<=' => '≤',
]
]);
}
protected function componentName()
{
return 'column-filter';
}
}
All contributions are welcomed. Please send a PR
The filter is loosely-based on 64Robots’s Date Filter and its custom filter selector component
This package is open-sourced software licensed under the MIT Licence