首页 > 解决方案 > Laravel 在 eloquent 查询期间使用计算

问题描述

我想查询所有净价低于一定金额的产品。在我的表中,我只存储总价和折扣,其中净价是总价和折扣之间的差额

在我的表中,我有以下列

产品表

name,
gross_price,
discount_amount

所以在我的查询中,我想获取所有gross_price - discount_amount小于一定数量的项目,如 100

所以目前正在使用雄辩的喜欢

Product::where('', '<', 100) //stuck on how to add new net_price in the query

我坚持如何将计算添加到我的查询中。如何在不需要向表中添加另一个字段的情况下添加计算?

标签: phplaravel

解决方案


尝试使用---

Product::where(\DB::raw('(gross_price - discount_amount)'),'<',100);

希望它会帮助你。


推荐阅读