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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article
Image Dominant Color and HEIC Support in Laravel 13.24 image

Image Dominant Color and HEIC Support in Laravel 13.24

Read article
Official Laravel Zed Extension: LSP for PHP & Blade image

Official Laravel Zed Extension: LSP for PHP & Blade

Read article
Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD image

Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD

Read article