orchidsoftware / platform

Orchid is a @laravel package that allows for rapid application development of back-office applications, admin/user panels, and dashboards.
https://orchid.software
MIT License
4.4k stars 646 forks source link

Table filter numberRange and WhereBetween error #2643

Open c-gross opened 1 year ago

c-gross commented 1 year ago

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

MercerMorning commented 1 year ago

@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