Laravel where() Cheat Sheet: The Complete Guide to Query Filtering

In this comprehensive guide, we’ll explore all Laravel where() methods, common use cases, best practices, examples, and performance tips.

Whether you’re a beginner or an experienced Laravel developer, this cheat sheet will help you write optimized and production-ready queries.

Laravel where() Cheat Sheet: The Complete Guide to Query Filtering Image

When working with databases in Laravel, the where() method is one of the most powerful and frequently used features of the Eloquent query builder. But did you know Laravel offers multiple types of where conditions that make your queries cleaner, faster, and more expressive?

 

What Is where() in Laravel?

The where() clause in Laravel is used to filter database results based on specific conditions. Laravel extends this feature with dozens of variations like:

  • whereIn()
  • whereBetween()
  • whereJsonContains()
  • whereColumn()
  • whereIntegerInRaw()
  • And many more…

Each method is designed for specific scenarios to improve readability and performance.


Laravel where() Cheat Sheet (With Examples)

where() & orWhere() – Basic Filtering

 
User::where('status', 'active')->get();
User::orWhere('role', 'admin')->get();

Use when: you need simple condition-based queries.


whereIn() & whereNotIn()

 
Post::whereIn('category_id', [1, 2, 3])->get();
Post::whereNotIn('id', [5, 6])->get();

Use when: you want to match multiple values.


whereIntegerInRaw() – High Performance for Large ID Lists

 
Order::whereIntegerInRaw('id', $bigList)->get();

Faster than whereIn() for large integer arrays because it avoids parameter binding overhead.


whereBetween() & whereNotBetween()

 
Transaction::whereBetween('amount', [100, 500])->get();

Use when: filtering within numeric or date ranges.


whereNull() & whereNotNull()

 
User::whereNull('email_verified_at')->get();

Useful for checking empty/undefined fields.


whereColumn() – Compare Two Columns

 
Invoice::whereColumn('due_amount', '>', 'paid_amount')->get();

Perfect for business logic like pending payments or mismatches.


whereRaw() – Raw SQL Conditions

 
User::whereRaw('YEAR(created_at) = ?', [2024])->get();

Use carefully, but still safe when using bindings.


whereExists() & whereSub() – Subqueries

 
User::whereExists(function ($q) {
    $q->select(DB::raw(1))
      ->from('orders')
      ->whereColumn('orders.user_id', 'users.id');
})->get();

Ideal for relational logic.


whereJsonContains() – JSON Search

 
Product::whereJsonContains('tags', 'featured')->get();

Use for JSON arrays in MySQL/PostgreSQL.


whereJsonOverlaps() – Check JSON Array Overlap

 
Event::whereJsonOverlaps('participants', [2, 3])->get();

Finds any matching element inside JSON arrays.


whereJsonLength()

 
Survey::whereJsonLength('answers', '>', 3)->get();

Useful for multi-answer questions or dynamic fields.


when() – Conditional Query Building

 
User::when($active, function ($q) {
    $q->where('status', 'active');
});

Cleaner than using if-else around queries.


Pro Tips for Writing Better where() Queries

1. Use whereIntegerInRaw() for large ID arrays

It’s faster and more memory-efficient than whereIn().

2. Debug queries with →toSql()

 
dd(User::where('status', 'active')->toSql());

3. Always index columns used in where()

Improves performance dramatically for large databases.

4. Use JSON-specific methods for JSON columns

They are optimized and cleaner than raw SQL.


Best Practices for Production-Ready Eloquent Queries

  • Use query scopes to organize complex where() logic.
  • Prefer whereColumn() for comparing columns instead of raw SQL.
  • Use chained where() for readability and clarity.
  • Avoid unnecessary whereRaw() unless required.
  • Always test queries with realistic data size.

Conclusion

Laravel’s where() methods are more than simple filters—they're powerful tools that help you write clean, expressive, and optimized queries.

By mastering all the variations (whereIn, whereJsonContains, whereBetween, whereIntegerInRaw), you can significantly improve both readability and performance in your application.

If you're working with Eloquent daily, bookmark this cheat sheet — it will save you hours of debugging and refactoring.

Do you Like?