Laravel自己的范围变量 [英] Laravel own variable in scope
本文介绍了Laravel自己的范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但是,我正在使用haversine公式来计算距离,这是正常的。我想隐藏距离超过该结果的max_radius字段的结果。
这是我的数据库方案。 p>
这是我使用的查询。
你可以看到我硬编码的距离(50)
public function scopeFitsDistance $查询,$ lat,$ lng)
{
返回$ query-> select(\DB :: raw(*,
(3959 * acos(cos(弧度(? )*
cos(弧度(lat))
* cos(弧度(lng) - 弧度(?)
)+ sin(弧度(?))*
sin
- > addBinding($ lat,'select')
- > addBinding($ lng,'select')
- > addBinding($ lat,'select')
- > having('distance','<',50); < ----------
}
但现在我'我想知道我该如何隐藏那个距离< max_radius是表中的一个字段。
以下内容不返回结果
- > have('distance','&','max_radius');
谢谢!
解决方案
HAVING
仅适用于 GROUP BY
您可以进行子选择,然后再使用 WHERE
子句。
I'm using the haversine formula to calculate a distance, this is working fine.
But I would like to hide results where the distance is greater then the max_radius field on that result.
This is my database scheme.
This is the query I'm using. You can see I hard coded the distance (50)
public function scopeFitsDistance($query, $lat, $lng)
{
return $query->select(\DB::raw("*,
( 3959 * acos( cos( radians(?) ) *
cos( radians( lat ) )
* cos( radians( lng ) - radians(?)
) + sin( radians(?) ) *
sin( radians( lat ) ) )
) AS distance"))
->addBinding($lat, 'select')
->addBinding($lng, 'select')
->addBinding($lat, 'select')
->having('distance', '<', 50); <----------
}
But now I'm wondering how I can hide results where that distance < max_radius, which is a field inside the table.
The following returns no results
->having('distance', '<', 'max_radius');
Thank you!
解决方案
HAVING
only works with GROUP BY
You can do a sub-select and then use a WHERE
clause instead.
这篇关于Laravel自己的范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文