Laravel Eloquent – Filtering Results and Eager Loaded Results

laravelPHP

So here is the use case:

You have users and they have many addresses. Say you want to filter and eager load on addresses (say…only US addresses). In order to do so you have to filter the eager load AND filter using whereHas so your result set is only those users with US addresses and only their US addresses are returned. This seems a bit clunky.

Here is an example:

(new App\User)
    ->with(['address' => function(){/* filter code */}])
    ->whereHas('address', function(){/* pretty much the same filter code */})
    ->get()

Is there a better way to do this? I feel like I am missing something.

Best Answer

You're confusing whereHas and with.

The with method will let you load the relationship only if the query returns true.

The whereHas method will let you get only the models which have the relationship which returns true to the query.

So here :

(new App\User)->with(['address' => function($query){/* filter code */}])->get();

will return all the users, but will only eager load the address when the filter code returns true.

And here :

(new App\User)->whereHas('address', function($query){/* filter code */})->get();

will return only the Users which have their relationship "address" returning true on the filter code.