philperusse / nova-column-filter by philperusse

A Laravel Nova column queryer filter
342,361
39
4
Package Data
Maintainer Username: philperusse
Maintainer Contact: philippe.perusse@outlook.com (Phil Pérusse)
Package Create Date: 2018-11-05
Package Last Update: 2020-04-16
Language: Vue
License: MIT
Last Refreshed: 2024-11-23 03:23:36
Package Statistics
Total Downloads: 342,361
Monthly Downloads: 6,716
Daily Downloads: 324
Total Stars: 39
Total Watchers: 4
Total Forks: 12
Total Open Issues: 12

Column Filter for Laravel Nova

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.

Demo

Demo

Installation

Run this command in your Laravel Nova project:

composer require philperusse/nova-column-filter

Usage

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',
            ]
        ]);
    }
}

Customization

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' => [
                '='     => '=',
                '>'     => '>',
                '>='    => '≥',
                '<'     => '&lt;',
                '<='    => '&le;',
            ]
        ]);
    }

    protected function componentName()
    {
        return 'column-filter';
    }
}

Contributions

All contributions are welcomed. Please send a PR

Authors

The filter is loosely-based on 64Robots's Date Filter and its custom filter selector component

License

This package is open-sourced software licensed under the MIT Licence