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

Doesn't seem to work for many to many relationships #70

Closed ItsAndy closed 6 years ago

ItsAndy commented 6 years ago

I might be missing something but this doesn't seem to be working on models with belongsToMany relationships. Am I wrong? Is there a different way to write it?

ItsAndy commented 6 years ago

Actually, I'm wrong. Because idiot.

One thing though. When using the $relations[] way of calling relationship model filters, is there any way to remap the attributes? So for example, if you want to search for 'name' on a model but also 'name' on a related model, I can't figure out a way to properly do that with the $relations array method. I want to delegate to all my model filters instead of copying them with the $this->related() method. Am I missing something obvious? (probably)

Example:

$input = [ 'name' =>'primary model name column', 'othername' => 'related model name column' ];

If the column names are both "name" then how can I use ...

$relations[ 'othermodel' => ['name'] ];

Tucker-Eric commented 6 years ago

As of now there is no way to alias an input key but I do plan on adding that feature.

In regards to filtering locally and on related, if a value in the $related array is also a local filter method then both will be called.

In your case, if your input has 2 keys to denote the difference between models they filter but share the same column name I would create an aliased named method on the related model filter that corresponds to the $relations array and then push that value into the input in the setup method.

So in your othermodel filter have a othername method and in your current mode's filter do:

protected $relations = [
    'othermodel' => ['othername']
];

public function setup()
{
    if($name = $this->input('name')) {
        $this->push('othername', $name);
    }
}
ItsAndy commented 6 years ago

Thanks, I'll give that a shot tomorrow. Great package though.