Laravel: Multiple Where And-OR Conditions with Example

Laravel: Multiple Where And-OR Conditions with Example

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.

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 - SelectInsert , 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:

...
    ->where('column','operator','value')
    ->where('column','operator','value')
...

Example:

Let's find the user with 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 grouped form:

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

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().

Syntax:

...
    ->where('column','operator','value')
    ->orWhere('column','operator','value')
...

laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

How to Hire Laravel Developers and How Much Does It Cost.

Want to make the most out of the latest Laravel technology? Here is your guide on how to hire the best Laravel developer, and at the right price. 

How to perform migration rollback in laravel

Sometimes we need to rollback one specific migration. Here, in this article we will discuss about the migration rollback in laravel.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→