Streamlining Relationship Queries with Laravel's whereRelation
The Evolution of Relationship Existence
Filtering models based on the state of their relationships is a fundamental task in any has method to check for basic existence or the more flexible whereHas method when specific column constraints were required. While powerful, whereHas often leads to "boilerplate bloat" because it requires a closure and a query builder instance, even for simple key-value comparisons. The whereRelation method, introduced in
Prerequisites
To follow along, you should have a baseline understanding of
Key Libraries & Tools
- Laravel: The primary web framework providing the Eloquent ORM.
- Eloquent ORM: The database abstraction layer used to interact with data as objects.
- Tinker: A REPL (Read-Eval-Print Loop) for interacting with your application's code and database.
Code Walkthrough: From Closures to Clean Lines
When you need to find
$airlines = Airline::whereHas('flights', function ($query) {
$query->where('is_delayed', true);
})->get();
This is functional but verbose. With whereRelation, we flatten the syntax into a single, readable line. The method accepts the relationship name as the first argument, the column name as the second, and the value as the third.
$airlines = Airline::whereRelation('flights', 'is_delayed', true)->pluck('name');
Under the hood, WHERE EXISTS SQL query. You get the same performance and the same result, but the code remains much easier to maintain and read at a glance.
Advanced Syntax & Comparisons
You aren't limited to simple equality checks. If you need to perform range comparisons or date checks, whereRelation supports the standard three-argument structure common in other Eloquent methods. For example, to find
$airlines = Airline::whereRelation(
'flights',
'expected_start_at',
'<',
now()->addDays(30)
)->get();
Tips & Gotchas
While whereRelation is perfect for simple conditions, stick to whereHas if you need to chain multiple complex where clauses or implement logical OR groups within the relationship query. Always use
