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
Eloquent Laravel - selectRaw() in whereHas()'s closure
提问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)