Laravel: Multiple Where And-OR Conditions with Example

By Parth Patel on Oct 07, 2020

While building your Laravel query whether using Eloquent Query or DB Query builder, you often have to use multiple where conditions.

Today, we will review the different syntax as well as few examples for it.

Remember: These conditions can be used in any kind of database query - Select, Insert , Update or Delete conditions as well as in sub-queries.

If you don't provide comparison operator in condition, by default = (equals) operator is used.

Let's talk about where() (which is AND condition) and orWhere() conditions:

Laravel multiple where conditions - [AND]:

By default, where conditions are chaining with AND operator. To join via OR operator, you will have to use orWhere which we will talk next.

1) Simple Syntax:



Let's find the user with name = "John Doe" and email = ""

$user = User::where('name','John Doe')->where('email','')->first();

2) Array Syntax:



Let's recreate our above example in grouped form:

$user = User::where([
					['name','John Doe'],

Laravel multiple where conditions - [OR]:

What if you want to use OR condition in your query? You will have to use orWhere() condition for addition query.

Note: First query cannot start with orWhere(). It has to be regular where().




$user = User::where('email','')->orWhere('email','')->first();

Above example means - Find me first user whose email is whether '' or ''

Group Multiple AND-OR-AND where conditions in Parentheses

What if you want to group multiple AND, OR & AND conditions in your query? When dealing with complex queries, you often will come up with this situation.

You cannot chain multiple AND-OR-AND conditions together directly. Remember - In SQL, you will have to use parentheses if you want to group such conditions.

But, in Laravel how would you replicate that?!

⇒ Using Closures


->where(function($query) {

Above, you can see that I have passed a closure function in outer where() condition - It encapsulates the inner conditions in parentheses.

Let's review one example:


I want to find all users who are active and ( whose email is or whose email is ) —> [I have used parenthese to demonstrate that no matter what's the case - user should be active]

$users = User::where('active','1')->where(function($query) {


Select * from users where active = '1' and
 (email = '' or email = '');

Note: You can nest multiple closures too though make sure to different variable names for each closure

So, we have covered pretty much all types of where() clauses with some simple examples. Let me know whether it helped you or not. If you have any doubts related to your query, or you cannot create any query, let me know in the comments.