TechvBlogs
Published in

TechvBlogs

Laravel Multiple Where Condition Example

Jun 20, 2022, Originally published at techvblogs.com ・2 min read

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:

...{{ADD_HERE}}
->where('column','operator','value')
->where('column','operator','value')
...

Example:

Let’s find the user with the name = “John Doe” and email = “john@example.com”

$user = User::where('name','John Doe')->where('email','john@example.com')->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','jdoe@example.com']
])->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','jdoe@example.com')->orWhere('email','jhon@example.com')->first();

The above example means — Find me the first user whose email is whether ‘jdoe@example.com’ or ‘jhon@example.com’.

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','jdoe@example.com')
->orWhere('email','johndoe@example.com');
})->get();

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

Thank you for reading this blog.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store