Use whereAny() and whereAll() to apply query conditions across multiple database columns without repetitive orWhere() closures.
When building search features that match a query string against first name, last name, email, and company, writing nested where() and orWhere() blocks is verbose and prone to operator precedence bugs.
Laravel provides whereAny() and whereAll().
Searching Across Any Column with whereAny()
use App\Models\User;
$search = 'punyapal';
// Generates: WHERE (first_name LIKE '%punyapal%' OR last_name LIKE '%punyapal%' OR email LIKE '%punyapal%')
$users = User::whereAny(
['first_name', 'last_name', 'email', 'username'],
'LIKE',
"%{$search}%"
)->get();
Enforcing Constraints on All Columns with whereAll()
// Generates: WHERE (rating > 4 AND score > 4 AND quality_index > 4)
$eliteProducts = Product::whereAll(
['rating', 'score', 'quality_index'],
'>',
4
)->get();
Summary
- Automatically wraps conditions in parentheses to preserve logical SQL operator precedence.
whereAny([...], '=', $val)combines columns withOR.whereAll([...], '=', $val)combines columns withAND.
Related Tips
View all tips →Find Models Without Related Records Using doesntHave() and whereDoesntHave()
Use doesntHave() and whereDoesntHave() to query models that have zero related child records.
Simplify Relationship Queries with whereRelation()
Replace verbose whereHas() closures with whereRelation() and orWhereRelation() for clean single-column relationship queries.