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

前端 未结 3 2022
孤城傲影
孤城傲影 2021-02-07 03:09

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

相关标签:
3条回答
  • 2021-02-07 03:51

    Option 1: ->whereRaw('column1 = column2')

    Option 2: When on sale buyer_id == null != seller_id, ->where('buyer_id', '=', null)

    0 讨论(0)
  • 2021-02-07 03:55

    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

    0 讨论(0)
  • 2021-02-07 04:00

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

    $same = Market::whereColumn('seller_id', 'buyer_id')->get();
    
    0 讨论(0)
提交回复
热议问题