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')



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);

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 Many To Many Polymorphic Relationship
Laravel 8 Many To Many Polymor...

In this tutorial we will see laravel 8 many to many polymorphic relationship. many to many polymorphic relationship more...

Read More


Google Map With Draggable Marker Example
Google Map With Draggable Mark...

In this example I will show you how to add a google map to your website, In many websites you can see they have added ma...

Read More


How To Validate Max File Size Using Javascript
How To Validate Max File Size...

This article will show us how to validate max file size using javascript. Many times we have a requirement to check...

Read More


Laravel tips: DB Models and Eloquent - Part 2
Laravel tips: DB Models and El...

Laravel, the PHP web application framework, is renowned for its elegant and efficient approach to handling databases thr...

Read More