Simplify Complex Conditions in Laravel Using whereAny()

Search across multiple columns without repeating orWhere().

  • 27 Jun, 2026
  • 27 Views

Simplify Complex Conditions in Laravel Using whereAny()

Searching across multiple columns is a common requirement.

Many developers write:

User::where('first_name', 'like', "%{$search}%")
    ->orWhere('last_name', 'like', "%{$search}%")
    ->orWhere('email', 'like', "%{$search}%")
    ->get();

It works...

But it quickly becomes repetitive.

The Cleaner Solution

Laravel provides:

whereAny()

Example:

User::whereAny(
    ['first_name', 'last_name', 'email'],
    'like',
    "%{$search}%"
)->get();

Laravel automatically generates the necessary OR conditions.

Real Project Example

Search products by multiple fields:

Product::whereAny(
    ['title', 'sku', 'barcode'],
    'like',
    "%{$keyword}%"
)->get();

Now users can search using:

  • Product name
  • SKU
  • Barcode

With a single condition.

Share: