Tucker-Eric / EloquentFilter

An Eloquent Way To Filter Laravel Models And Their Relationships
http://tucker-eric.github.io/EloquentFilter
MIT License
1.72k stars 120 forks source link

Using different comparisons #58

Closed liran-co closed 6 years ago

liran-co commented 6 years ago

Is there a recommended way to use different comparison operators? For example, if I wanted to filter potential_volume is <, > or =. In the examples in the documentation, the comparison operator is implied in the function. Perhaps something like this:

[
  'potential_volume' => '>100',
  'name' => 'james%'
]
Tucker-Eric commented 6 years ago

You could do it a few ways.

The ways I have done it in the past are:

Like your example:

[
  'potential_volume' => '>100',
  'name' => 'james%'
]
public function potentialVolume($volume)
{
    return $this->where('potential_volume', $volume[0], substr($volume, 1));
}

public function name($name)
{
    return $this->where('name', strpos($name, '%') !== false ? 'LIKE' : '=', $name);
}

Or with an array of values:

Url: /name[0]=like&name[1]=james&potential_volume[0]=>&potential_volume[1]=100 So the methods would be:

public function potentialVolume($volume)
{
    return $this->where('potential_volume', $volume[0], $volume[1]);
}

public function name($name)
{
    return $this->where('name', $name[0], $name[1]);
}
liran-co commented 6 years ago

Awesome, thanks Eric!

liran-co commented 6 years ago

Opening this back up to give context to my follow up question. In your example above regarding using an array of values. Is there a way to perform multiple filters of the same type (ex: multiple name filters, where name starts with "John" OR name is "James")?

It would be useful to have the comparison operators built in for this reason similar to https://github.com/jedrzej/searchable