在laravel中找到两行通过口才有相等值的列? [英] Find two column in laravel which have equal values via Eloquent?
问题描述
我正在一个项目中,我有一个表市场,该表市场具有buyer_id列和seller_id列.当卖方在市场上投放商品时,seller_id和buyer_id相同,这意味着该产品现在要出售.出售后,buyer_id更改为购买该产品的任何人.
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.
现在我的应用程序中展示所有待售产品的位置,我正在通过Eloquent进行此查询:
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();
我只想出售与登录用户不同的产品,并且我只想拥有Seller_id和Buyer_id相同的那些产品.现在,第二个where
语句中的问题在于,它正在将Seller_id与字符串'buyer_id'
进行比较,这不是我想要的.我只能以什么样的方式获取buyer_id等于seller_id
的记录.
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
.
推荐答案
您需要使用whereRaw
来做到这一点:
You need to use whereRaw
to do it:
$market_records = Market::where('seller_id', '!=', Auth::user()->id)
->whereRaw('seller_id = buyer_id')->get();
任何寻求此解决方案的人都请记住,因为Laravel 5.2可以使用whereColumn
方法代替,因此上述 Laravel 5.2及更高版本中的代码如下所示:
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();
您可以在此提交
这篇关于在laravel中找到两行通过口才有相等值的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!