Laravel Tutorials

Eloquent Relationship Queries in Laravel with whereRelation

Published
Eloquent Relationship Queries in Laravel with whereRelation image

Laravel's whereRelation method streamlines the process of filtering models based on their relationships' attributes. This elegant approach replaces complex subqueries and joins with a more readable and maintainable syntax.

This feature proves especially valuable when building complex filters in e-commerce platforms, content management systems, or any application where models are interconnected and filtering based on related data is crucial.

Post::whereRelation('comments', 'is_approved', true)->get();

Here's an example of building a course filtering system:

<?php
 
namespace App\Http\Controllers;
 
use App\Models\Course;
use Illuminate\Http\Request;
 
class CourseController extends Controller
{
public function browse(Request $request)
{
$courses = Course::query();
// Filter by instructor rating
if ($request->has('top_rated')) {
$courses->whereRelation(
'instructor',
'rating',
'>=',
4.5
);
}
 
// Filter by recent student reviews
if ($request->has('well_reviewed')) {
$courses->orWhereRelation(
'reviews',
'created_at',
'>=',
now()->subDays(30)
);
}
 
// Filter by active discussion
if ($request->has('active_discussion')) {
$courses->whereRelation(
'discussions',
'last_activity',
'>=',
now()->subDays(7)
);
}
return $courses->with(['instructor', 'reviews'])
->latest()
->paginate();
}
}

The query builder creates efficient SQL based on your relationship conditions:

// Filters courses with:
// - Highly rated instructors (4.5+)
// - OR recent reviews
// - AND active discussions
$courses = Course::whereRelation('instructor', 'rating', '>=', 4.5)
->orWhereRelation('reviews', 'created_at', '>=', now()->subDays(30))
->whereRelation('discussions', 'last_activity', '>=', now()->subDays(7))
->get();

WhereRelation provides a clean, expressive way to filter models based on relationship attributes, leading to more maintainable code.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article