philperusse / nova-column-filter

A Laravel Nova column queryer filter
MIT License
39 stars 12 forks source link

Empty drop-down lists #7

Closed ahmedkandel closed 5 years ago

ahmedkandel commented 5 years ago

Hi Philippe,

The package was working correctly but later on found that the columns and operators drop-down are empty:

screenshot_1

Maybe the reason is a Nova update currently I have the latest version v1.3.2

here is the filter code:

namespace App\Nova\Filters;

use Illuminate\Http\Request;
use philperusse\Filters\ColumnFilter as Filter;

class UserColumnFilter extends Filter
{
    /**
     * Set the displayable name of the filter.
     *
     * @return string
     */
    public function name()
    {
        return __('User Query');
    }

    /**
     * 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;
        if($args[1] == 'LIKE') $args[2] = '%' . $args[2]. '%'; //Add the surrounding % if needed;

        return $query->where(...$args);
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return array_merge(parent::options($request), [
            'columns' => [
                'id' => 'ID',
                'code' => 'Code',
                'name' => 'Name',
            ],
            'operators' => [
                'LIKE' => 'Contains',
                '=' => '=',
            ]
        ]);
    }
}

Thanks.

ahmedkandel commented 5 years ago

The issue was resulting due to changes in Nova v1.3.2 a possible fix #8