[英]Is there any way to get rows that satisfy a relation between it's columns in Laravel?
I have a MySQL table named prices
which looks like:我有一个名为
prices
的 MySQL 表,如下所示:
Name | price | amount | total
-------------------------------
name1 | 15 | 2 | 17
name2 | 20 | 3 | 20
name3 | 25 | 4 | 29
name4 | 30 | 5 | 35
I want to get the names which specify that the sum of the price and amount is equal to the total, which are name1
and name3
in the previous table.我想获取指定价格和金额之和等于总数的名称,即上表中的
name1
和name3
。
I already did that as SQL query like the following SELECT Name FROM prices WHERE price + amount = total
.我已经这样做了,因为 SQL 查询如下
SELECT Name FROM prices WHERE price + amount = total
。
Is any way to do it in Laravel Eloquent if I have a prices
model?如果我有 model 的
prices
,有什么办法可以在 Laravel Eloquent 中做到这一点?
I don't think eloquent has some magic method for this.我不认为 eloquent 对此有什么神奇的方法。 But you can always use
whereRaw
with eloquent as shown below:但是您始终可以将
whereRaw
与 eloquent 一起使用,如下所示:
Prices::select('name')->whereRaw('price + amount = total')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.