2

我的以下代码是这样的:

$places = DivePlace::selectRaw("*,(st_distance_sphere( POINT(".$lon.",".$lat.") ,  point(lon, lat))/1000) as distance")
    ->havingRaw("distance < ".$radius)
    ->orderBy("distance")
    ->paginate(10);

没有“haveRaw”,一切都很好。添加后出现如下错误:

SQLSTATE [42S22]:未找到列:1054 '有子句'中的未知列'距离'(SQL:从dive_places 距离 < 300 中选择计数(*)作为聚合)

有什么解决办法吗?

4

2 回答 2

2
->where(DB::raw("(ST_Distance_Sphere(POINT(".$lon.",".$lat."), POINT(lon,lat))/1000)"), '<', 200)

代替->havingRaw("(st_distance_sphere( POINT(?, ?) , point(lon, lat))/1000) < ?", [$lon, $lat, $radius])

于 2015-12-01T16:14:51.747 回答
0

您需要重复距离定义,因为分页 Laravel 仅count(*)用于列,所以它应该是:

$places = DivePlace::selectRaw("*,(st_distance_sphere( POINT(".$lon.",".$lat.") ,  point(lon, lat))/1000) as distance")
    ->havingRaw("(st_distance_sphere( POINT(".$lon.",".$lat.") ,  point(lon, lat))/1000) < ".$radius)
    ->orderBy("distance")
    ->paginate(10);

您还可以对查询使用绑定,因此更好的是:

$places = DivePlace::selectRaw("*,(st_distance_sphere( POINT(?, ?) ,  point(lon, lat))/1000) as distance",[$lon, $lat])
    ->havingRaw("(st_distance_sphere( POINT(?, ?) ,  point(lon, lat))/1000) < ?", [$lon, $lat, $radius])
    ->orderBy("distance")
    ->paginate(10);
于 2015-11-29T12:24:45.480 回答