例如,我有一张桌子,
id apple orange
1 1 1
2 1 2
3 2 2
我希望得到$ apple + $ orange>的项目$门槛。例如,如果$ threshold = 3,则答案为No.3。如何在Laravel查询构建器中执行此操作?
我尝试过这个问题的过滤器():How to add two columns value and perform where condition in laravel eloquent
$set = collect($set)->filter(function ($val) use ($threshold) {
return $val->apple + $val->orange > $threshold;
})->values()->all();
它确实有效,但对我来说看起来真的很难......还有其他方法可以解决这个问题吗?
答案 0 :(得分:3)
我将您的要求解释为等同于以下查询:
SELECT *
FROM fruits
WHERE apple + orange > $threshold
如果是这样,那么您可以尝试在这种情况下使用whereRaw
:
DB::table('fruits')
->whereRaw("apple + orange > ?", array($threshold));
->get();