Laravel 9 whereColumn Query Example

Websolutionstuff | Oct-21-2022 | Categories : Laravel

In this article, we will see laravel 9 whereColumn() query example. The whereCoulmn method is used to verify whether two columns are equal or not. So, we will learn whereColumn in laravel 9.

Laravel also provides orWhereColumn() method. So, you can also use whereColumn() with orWhereColumn() method in laravel.

So, let's see laravel 9 whereColumn() and compare two columns in laravel 9.

Syntax of whereColumn()

whereColumn('column name', 'operator', 'compare column name')

 

Example 1:

In this example, we will retrieve all those employee's records which are both column equals.

$employee = DB::table('employee')
                ->whereColumn('first_name', 'last_name')
                ->get();

 

Example 2:

You may also pass a comparison operator to the whereColumn method.

$users = DB::table('users')
                ->whereColumn('updated_at', '>', 'created_at')
                ->get();

 

Example 3:

You may also pass an array of column comparisons to the whereColumn method. These conditions will be joined using the and operator.

$users = DB::table('users')
                ->whereColumn([
                    ['first_name', '=', 'last_name'],
                    ['updated_at', '>', 'created_at'],
                ])->get();

 


You might also like:

Recommended Post
Featured Post
REST API In Laravel
REST API In Laravel

In this article, we will explore the world of RESTful APIs in the context of Laravel, encompassing versions 8, 9, and 10...

Read More

Aug-26-2020

How To Convert PHP Array To JSON Object
How To Convert PHP Array To JS...

In this article, we will explore the process of converting a PHP array into a JSON object. We'll achieve this transf...

Read More

Jul-08-2020

Laravel 8 Send Mail using Queue
Laravel 8 Send Mail using Queu...

In this tutorial I will show you laravel 8 send mail using queue, many time we can see some process to take mo...

Read More

Sep-24-2021

Laravel tips: DB Models and Eloquent
Laravel tips: DB Models and El...

In the realm of web development, an efficient and robust data handling mechanism is paramount. Laravel, a PHP web applic...

Read More

Oct-11-2023