This post is more than a year old. There is a chance the content is outdated.

Refactoring with Laravel's new whereRelation methods

Refactoring with Laravel's new 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.

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

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

1User::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:

1User::whereHas('posts', function ($query) {
2 $query->notPublished();

Or nested where:

1User::whereHas('posts', function ($query) {
2 $query
3 ->whereHas('tags', function ($query) {
4 $query->where('name', 'Laravel');
5 })
6 ->where('published_at', '>', now());

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

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

By Anthony Rappa

Hello! I'm a full stack developer from Long Island, New York. Working mainly with Laravel, Tailwind, Livewire, and Alpine.js (TALL Stack). I share everything I know about these tools and more, as well as any useful resources I find from the community. You can find me on GitHub and LinkedIn.