Laravel Multiple Where Condition Example - TechvBlogs

Laravel Multiple Where Condition Example

In this tutorial, you will learn how to add multiple where clauses using Laravel Eloquent.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 year ago

TechvBlogs - Google News

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.

Read Also: How To Optimize Laravel for Performance

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

Comment


Note: All Input Fields are required.