Header Ads

How to make Laravel nova filters step by step step

Most of the time you meet on your application after login, if the filter is used for filter type of data that you want to show on the list view. but the filter in nova "select" filter using the PHP artisan command to create a filter. 


    php artisan nova:filter UserType


after you generate you will see in the folder  app/Nova/Filters


On the Nova filter file, you will be seeing the method apply and option. Apply filter method is given query and option is an available to list to select for filter. 

    <?php

    namespace App\Nova\Filters;

    use Illuminate\Http\Request;
    use Laravel\Nova\Filters\BooleanFilter;

    class UserType extends BooleanFilter
    {
    /**
     * 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)
    {
        // $value = ['admin' => true / false, 'editor' => true / false]

        return $query->where('is_admin'$value['admin'])
                     ->where('is_editor'$value['editor']);
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return [
            'Administrator' => 'admin',
            'Editor' => 'editor',
        ];
    }
}

No comments:

Powered by Blogger.