Using whereHas() with HasManyThrough Relationships

I’ve discovered an issue with Eloquent in which using the whereHas() method with a HasManyThrough relationship returns the wrong results.

For instance, if I have the following model structure:

- hasMany Lead
- hasManyThrough Purchase (through Lead)
- belongsTo Business
- hasOne Purchase
- belongsTo Lead

the following code will yield unexpected results:

$businesses = Business::whereHas('purchases', function($query)
$query->where( // insert where clause here );

I’ve sent a pull request to fix the issue but it needs to be discussed, so for now you can do use nested whereHas() calls like so:

$businesses = Business::whereHas('leads', function($query)
$query->whereHas('purchase', function($query)
$query->where( // insert where clause here );
Made with ❤ by Chris Hayes