Support the ongoing development of Laravel.io →
Database Eloquent

Hi all :),

First of all : I opened a help paste here describing the core of the problem.

I did many research on the problem but didn't find anything that match exactly my purpose... So I hope you guys can help me a bit :)

I'm trying to filter results of a nested eager loaded query with this kind of stucture (see the help link for real code)

$results = MyModel::with(array(
   'my',
   'my.nested',
   'my.nested.stuff' => function($query) use ($myfilter) {
       $query->with(array('other', 'relations'))
       //The filter has to happen here
                  ->whereIn('column_to_filter', $myfilter);
    }
));

The structure output is OK but when I want to filter results within the nested function, the SQL request made is like so :

SELECT * FROM stuff where nested IN (?,?...) AND column_to_filter IN (?, ?...)

Which is clearly ruining the filter. Without the first where, it would work like a charm.

In the docs, it says that you can have an conditional eager load, but what about a conditional NESTED eager load ?

I know that I can do it with join but I wanna keep the nested structure of results that the eager load function provides plus the easy syntax, and I don't really see simples ways to make it.

Thanks !

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Charkhan charkhan Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.