Laravel 5 get all records from table where two columns are equal

Laravel 5 get all records from table where two columns are equal

In this post we will give you information about Laravel 5 get all records from table where two columns are equal. Hear we will give you detail about Laravel 5 get all records from table where two columns are equalAnd how to use it also give you demo for it if it is necessary.

In Laravel, you can easily compare two column using simple whereColumn in query builder.

If you need to get all records from table where value of two columns are equal then you can easily get it in Laravel by using whereColumn.

There may be so many condition where you can use this method with comparison operator.

You can also pass an array of multiple conditions in this method but make sure all conditions passed within array will be joined using and operator.



Example 1:

In this example, we will get all products where price of both column (column main_price and offer_price) will be equal.

  1. $products= DB::table('products')
  2. ->whereColumn('offer_price','main_price')
  3. ->get();
$products = DB::table('products')
                ->whereColumn('offer_price', 'main_price')
                ->get();


Example 2:

In this example, we will get all products where price of column main_price will be higher than column offer_price.

  1. $products= DB::table('products')
  2. ->whereColumn('main_price','>','offer_price')
  3. ->get();
$products = DB::table('products')
                ->whereColumn('main_price', '>', 'offer_price')
                ->get();  


Example 3:

In this example, we will pass multiple condition in array.

  1. $products= DB::table('products')
  2. ->whereColumn([
  3. ['offer_price','=','main_price'],
  4. ['updated_at','>','created_at']
  5. ])->get();
$products = DB::table('products')
                ->whereColumn([
                    ['offer_price', '=', 'main_price'],
                    ['updated_at', '>', 'created_at']
                ])->get();




You can use above example whenever you need to compare two columns in Laravel application.

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement Laravel 5 get all records from table where two columns are equal. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

38  +    =  44

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US