Clean Relationship Filtering in Laravel Using withWhereHas()

Filter and eager load relationships in one expressive query.

  • 30 Apr, 2026
  • 207 Views

Clean Relationship Filtering in Laravel Using withWhereHas()

In Laravel, when working with relationships, you often need to:

  • Filter parent models
  • Load only specific related records

Laravel provides a clean solution: withWhereHas().

๐Ÿ”น Basic Usage

User::withWhereHas('orders', function ($q) {
    $q->where('status', 'paid');
})->get();

๐Ÿ‘‰ This will:

  • Return users who have paid orders
  • Load only those paid orders

๐Ÿ”น Using with Multiple Conditions

User::withWhereHas('orders', function ($q) {
    $q->where('status', 'paid')
      ->where('amount', '>', 1000);
})->get();

๐Ÿ‘‰ Filters both user and related orders together.

๐Ÿ”น Real Project Example

Imagine showing users with premium purchases:

$users = User::withWhereHas('orders', function ($q) {
    $q->where('plan', 'premium');
});

Now:

  • Only users with premium orders are returned
  • Only premium orders are loaded

๐Ÿ”น Nested Relationship Usage

User::withWhereHas('orders.items', function ($q) {
    $q->where('type', 'digital');
})->get();

๐Ÿ‘‰ Works even for deeper relationships.

๐ŸŽฏ Why Use withWhereHas()

  • Keeps conditions in one place
  • Avoids duplication
  • Improves readability
  • Reduces mistakes in queries

๐Ÿง  When to Use It

Use it when:

  • Filtering relationships
  • Eager loading with conditions
  • Building APIs with relational data

Share: