Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find two column in laravel which have equal values via Eloquent?

I am working on a project where I have a table market which has a buyer_id column and a seller_id column. When a seller puts something on the market, the seller_id and buyer_id are the same which means that the product is for sale right now. After the sale, the buyer_id changes to whoever bought the product.

Now the place in my application where I am showing all the products up for sale I am doing this query via Eloquent:

$market_records = Market::where('seller_id', '!=', Auth::user()->id)->where('seller_id', '=', 'buyer_id')->get(); 

I want only the products up for sale which are not by the same user who has logged in and I want to have only those products where the seller_id and buyer_id is same. Now the problem in the second where statement is that the that it is comparing the seller_id to the string 'buyer_id' which is not what I want. What is way with which I can only fetch the records where the buyer_id is equal to the seller_id.

like image 678
Rohan Avatar asked Dec 04 '14 09:12

Rohan


People also ask

How can I compare two fields in Laravel?

Laravel 5 introduces “whereColumn()” in Query Builder, that way we can compare two column-like simples where condition. We sometimes require to check this type of condition. you can use the “wherecolumn()” eloquent function in laravel 6, laravel 7, and laravel 8 applications.

Which is better query builder or eloquent?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.

What does get () do in Laravel?

This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.

Can we use join in eloquent Laravel?

Eloquent power joinsAdd some Laravel magic to your Eloquent joins. If you have some experience using databases, it is very likely you have used joins at least once in your career. Joins can be used for a bunch of different reasons, from selecting data from other tables to limiting the matches of your query.


2 Answers

You need to use whereRaw to do it:

$market_records = Market::where('seller_id', '!=', Auth::user()->id)                          ->whereRaw('seller_id = buyer_id')->get(); 

Anyone looking for this solution keep in mind since Laravel 5.2 it's possible to use whereColumn method instead, so above code in Laravel 5.2 and up could look like this:

$market_records = Market::where('seller_id', '!=', Auth::user()->id)                          ->whereColumn('seller_id', 'buyer_id')->get(); 

You can find details in this commit

like image 108
Marcin Nabiałek Avatar answered Sep 20 '22 21:09

Marcin Nabiałek


In recent Laravel versions you can use whereColumn (docs):

$same = Market::whereColumn('seller_id', 'buyer_id')->get(); 
like image 35
bryceadams Avatar answered Sep 20 '22 21:09

bryceadams