
Laravel whereRelation(): Replace Unnecessary whereHas() Callbacks
When a relationship filter needs one simple condition, whereRelation() removes the extra whereHas() callback while keeping the same query intent explicit.
For a simple condition on a related model, whereHas() can add more ceremony than actual query logic.
When you only need to check relationship existence with one basic condition, whereRelation() expresses the same intent with less callback noise.
Replace a Simple whereHas() Callback
Post::query() ->whereHas( 'author', fn ($query) => $query->where('active', true), ) ->get();Post::query() ->whereRelation( 'author', 'active', true, ) ->get();Here the callback does not contain separate complex logic. It only forwards one condition to the relationship query, so whereRelation() makes the actual filter easier to see.
The intent stays the same: return only Post models for which a related author exists and matches the condition.
You Can Pass an Operator
Post::query() ->whereRelation( 'author', 'rating', '>=', 4, ) ->get();The shape is close to a normal where(): relation, column, operator, and value. For equality, you can omit the explicit operator.
When whereHas() Is Still Better
Post::query() ->whereHas( 'author', fn ($query) => $query ->where('active', true) ->where('verified', true) ->whereNotNull('email_verified_at'), ) ->get();The useful rule is not to replace every whereHas(). If the callback exists only to hold one where(), check whether whereRelation() communicates the same intent more directly.




