Eloquent Laravel - selectRaw() 在 whereHas() 的闭包中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28785288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:02:22  来源:igfitidea点击:

Eloquent Laravel - selectRaw() in whereHas()'s closure

phplaraveleloquent

提问by Joshua Hansen

I want to select sum of points and pass it into where clause. Following is my code:

我想选择点的总和并将其传递给 where 子句。以下是我的代码:

    $query->whereHas('evaluations', function ($q) use ($params)
    {
        $q->selectRaw('sum(evaluations.point) as points')
            ->where('points', '>=', $params->point);
    });

But it alerts Column not found: 1054 Unknown column 'points' in 'where clause'

但它提醒 Column not found: 1054 Unknown column 'points' in 'where clause'

回答by frin

You can't do a WHERE for columns created with SELECT. You have to use HAVING statement like ->having('points', '>=', $params->point)

您不能对使用 SELECT 创建的列执行 WHERE。您必须使用 HAVING 语句,例如->having('points', '>=', $params->point)