Laravel new Relation methods

Say hello to new Laravel whereRelation methods

Prior to this simple PR, we needed verbose closures to do simple queries on relationships. Now it can be done on one line with these simple helpers.

This is how you would currently query the relationship on a model to narrow down your results.

// Get me all users who have posts that are to be published in the future.
User::whereHas('posts', function ($query) {
    $query->where('published_at', '>', now());

With this new PR, we can now refactor and simplify the above to the following:

User::whereRelation('posts', 'published_at', '>', now())->get();

As you can see, we have collapsed the closure into the parameter list of the whereRelation method.

With this PR, you also have access to orWhereRelation, whereMorphRelation, and orWhereMorphRelation.

It is worth noting that because it uses a where clause under the hood, it would not be possible to use scopes:

User::whereHas('posts', function ($query) {

Or nested where:

User::whereHas('posts', function ($query) {
        ->whereHas('tags', function ($query) {
            $query->where('name', 'Laravel');
        ->where('published_at', '>', now());

Though you should be able to use nested relations if you don't have any intermediate where clauses:

User::whereRelation('posts.tags', 'name', 'Laravel')->get();

Laravel Eloquent Relation