Laravel 9 orWhere Condition Example

Websolutionstuff | Oct-13-2022 | Categories : Laravel

In this article, we will see the laravel 9 orWhere condition example. Where condition is joined together using the and operator. The orWhere condition is joined together using the or operator. The orWhere method is the same as the where() method. So, we will learn how to write multiple where conditions with orWhere in laravel 9, and also we will see SQL query of or condition.

The orWhere condition has three arguments, the first argument is the name of the column. The second argument is an operator. The third argument is the value to compare against the column's value.

So, let's see orWhere conditions in laravel 9.

Syntax of the orWhere() condition.

orWhere(Column Name, Operator, Value);

 

Example 1:

In this example, we will get the records using SQL query with OR condition.

SELECT * FROM users WHERE salary > 20000 OR role='software engineer';

 

Example 2:

In this example, we will retrieve records of the user's who has votes greater than 80 or whose name is dell.  

$users = DB::table('users')
                    ->where('votes', '>', 80)
                    ->orWhere('name', 'dell')
                    ->get();

 

 

Example 3:

 In this example, we will see the orWhere() condition with a subquery.

$users = DB::table('users')
            ->where('votes', '>', 80)
            ->orWhere(function($query) {
                $query->where('name', 'dell')
                      ->where('votes', '>', 50);
            })
            ->get();

The example above will produce the following SQL.

select * from users where votes > 80 or (name = 'dell' and votes > 50)

 

Example 4:

In this example, we will retrieve data using the where() and orWhere() with the model.

$users = User::where('is_active','=', true)->orWhere('plan','!=','basic')->get();

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Highcharts Example Tutorial
Laravel 8 Highcharts Example T...

Hello guys, In this tutorial we will see laravel 8 highcharts example tutorial. you will learn how to imple...

Read More

Jul-02-2021

How to Generate QR Code in Node.js
How to Generate QR Code in Nod...

In this example we will see how to generate QR Code in Node.js application. In this example we will use qrcode npm...

Read More

Sep-20-2021

Character Count In Textarea
Character Count In Textarea

In this article, we will explain to you how to count characters from textarea. many times a client has requirements...

Read More

Jul-08-2020

Laravel 9 CRUD With Image Upload Example
Laravel 9 CRUD With Image Uplo...

In this article, we will see the laravel 9 crud with an image upload example. Here, we will learn how to image upload wi...

Read More

Dec-09-2022