laravel/framework — pull request #6726 by jarektkaczyk (2014-12-19 01:57:42+00:00)
Title: improve has for nested relations

**Fixes lack of consistency when working with nested relations:**
```
// eager loading works with dot nested relations:
Model::with('closeRelation.farRelation.evenFurtherRelation')->get();

// but has filter does not:
Model::has('closeRelation.farRelation.evenFurtherRelation')->get();
// PHP Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to undefined method Illuminate\Database\Query\Builder::closeRelation.farRelation.EvenFurtherRelation()'
```

This PR allowes you to do the same with `has` (and whereHas, orHas etc):
```
Model::has('closeRelation.farRelation.evenFurtherRelation')->get();
```

The query will be **exactly the same** as when using this piece, which is required now, and it sucks:
```
Model::whereHas('closeRelation', function ($q) {
$q->whereHas('farRelaton', function ($q) {
$q->has('evenFurtherRelation');
});
})->get();
```

In order to achieve this it is required to add `relation count query` constraints in reversed order. This is solved by using callback closure that gets its own reference and calls itself recursively.

There are tests added, but in fact they are simply comparing and proving, that the method result is identical to the query generated using currently available method, with nested `whereHas` calls.