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

BelongsToMany and paginateFilter() destroys pivot #43

Closed Cannonb4ll closed 6 years ago

Cannonb4ll commented 6 years ago

When using the method paginateFilter() on a model with pivot table, this pivot table is not present inside the model. When changing back to paginate() the pivot model is back.

companies - company_user - users

In this case, if I would get all the users inside a company, and there would be any additional data inside the pivot table, it would be added inside the 'relations' object inside the model, but this is not present.

Tucker-Eric commented 6 years ago

Can you provide a code example?

Tucker-Eric commented 6 years ago

Closing due to paginateFilter() calls the builder's paginate() method and this sounds like an issue with the query itself and not the paginateFilter() method.

We can reopen if there is an example that will replicate this behavior.

rayronvictor commented 6 years ago

@Cannonb4ll you can use the second parameter to specify what you want: paginateFilter(perPage, ['*']) or paginateFilter(perPage, ['companies.*']) or paginateFilter(perPage, ['companies.*', 'company_user.*']) or paginateFilter(perPage, ['companies.*', 'company_user.*', 'user.*'])

Cannonb4ll commented 6 years ago

@Tucker-Eric Create the following relation:

user -> company_user <- companies

The table company_user is a pivot table, if I do the following methods, see the difference:

paginate <- Laravel's original method:

$companies = auth()->user()->companies()
    ->latest()
    ->filter(request()->all())
    ->paginate();

dd($companies->first());

img


paginateFilter <- This package's method:

$companies = auth()->user()->companies()
    ->latest()
    ->filter(request()->all())
    ->paginateFilter();

dd($companies->first());

img

You can see, the relations are now empty when using paginateFilter()

Tucker-Eric commented 6 years ago

@Cannonb4ll I was able to reproduce this and it should be fixed in https://github.com/Tucker-Eric/EloquentFilter/commit/5583402dd83234c1dcada8d42a16a70e9fa94060

Are you able to pull the latest (dev-master) and confirm this is resolved?

Cannonb4ll commented 6 years ago

@Tucker-Eric Confirmed and is resolved with dev-master!

Tucker-Eric commented 6 years ago

Awesome! Updated in https://github.com/Tucker-Eric/EloquentFilter/releases/tag/1.3.2