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
Option 1: ->whereRaw('column1 = column2')
Option 2: When on sale buyer_id == null != seller_id, ->where('buyer_id', '=', null)
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
In recent Laravel versions you can use whereColumn
(docs):
$same = Market::whereColumn('seller_id', 'buyer_id')->get();