Laravel Multiple Where Condition Example
In this post we will give you information about Laravel Multiple Where Condition Example. Hear we will give you detail about Laravel Multiple Where Condition Example And how to use it also give you demo for it if it is necessary.
The Eloquent ORM included with Laravel provides you with an easy way of interacting with your database. This simplifies all CRUD (Create, read, update, and delete) operations and any other database queries.
While building your Laravel query whether using Eloquent Query or DB Query builder, you often have to use multiple where conditions.
Laravel provides simple eloquent and query builder methods to query into a database. The where() method is the basic clause to query into the database. We almost need a where clause in every query.
Throughout the tutorial, you will learn how to use multiple where conditions in Laravel eloquent and query builder. You can use multiple where conditions to filter multiple columns into a database.
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 about next.
1) Simple Syntax:
...
->where('column','operator','value')
->where('column','operator','value')
...
Example:
Let’s find the user with the name = “John Doe” and email = “[email protected]”
$user = User::where('name','John Doe')->where('email','[email protected]')->first();
2) Array Syntax:
....
->where([
['column','operator','value'],
['column','operator','value'],
])
...
Example:
Let’s recreate our above example in the grouped form:
$user = User::where([
['name','John Doe'],
['email','[email protected]']
])->first();
Laravel multiple where conditions – [OR]:
In case you want your clauses to be joined with OR
rather than AND
, you could use the orWhere
method:
Syntax:
...
->where('column','operator','value')
->orWhere('column','operator','value')
...
Example:
$user = User::where('email','[email protected]')->orWhere('email','[email protected]')->first();
The above example means – Find me the first user whose email is whether ‘[email protected]’ or ‘[email protected]’.
Group Multiple AND-OR-AND where conditions
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.
Syntax:
...
->where(function($query) {
$query->where('column','operator','value')
->orWhere('column','operator','value');
})
...
Above, you can see that I have passed a closure function in the outer where() condition – It encapsulates the inner conditions in parentheses.
Example:
$users = User::where('status','active')->where(function($query) {
$query->where('email','[email protected]')
->orWhere('email','[email protected]');
})->get();
Note: You can nest multiple closures too though make sure to different variable names for each closure
Thank you for reading this blog.
Also see: How To Optimize Laravel for Performance
. .
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.
Hope this code and post will helped you for implement Laravel Multiple Where Condition Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs