platform icon indicating copy to clipboard operation
platform copied to clipboard

Table filter numberRange and WhereBetween error

Open c-gross opened this issue 2 years ago • 1 comments

The new Orchid 14 $allowedFilters configuration has an issue: Using a table filter TD::FILTER_NUMBER_RANGE and $allowedFilters WhereBetween only works when a min and max value is given.

If only one value is set (min or max) it will run in an SQL error. ?filter[stock][max]=5

SQLSTATE[HY093]: Invalid parameter number select count(*) as aggregate from products where stock between 5 and ?

Tested with Orchid 14.2.1

c-gross avatar Jun 16 '23 10:06 c-gross

@c-gross Hello! This filter uses WHERE BETWEEN sql command. This one won't work without both values. You may create custom filter like this:

class WhereMinMax extends BaseHttpEloquentFilter
{
    public function run(Builder $builder): Builder
    {
        $params = $this->getHttpValue();
        if (isset($params['min'])) {
            $builder->where($this->column, '>', $params['min']);
        }
        if (isset($params['max'])) {
            $builder->where($this->column, '<', $params['max']);
        }
        return $builder;
    }
}

It works correctly with TD::FILTER_NUMBER_RANGE

MercerMorning avatar Jul 29 '23 11:07 MercerMorning