laravel having: Column not found

后端 未结 2 781
夕颜
夕颜 2020-12-20 03:38

my following code is like this:

$places = DivePlace::selectRaw(\"*,(st_distance_sphere( POINT(\".$lon.\",\".$lat.\") ,  point(lon, lat))/1000) as distance\")         


        
相关标签:
2条回答
  • 2020-12-20 03:49

    You need to repeat distance definition because for pagination Laravel uses only count(*) for columns, so it should be:

    $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);
    

    You could also use bindings for the query, so better would be:

    $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);
    
    0 讨论(0)
  • 2020-12-20 03:52
    ->where(DB::raw("(ST_Distance_Sphere(POINT(".$lon.",".$lat."), POINT(lon,lat))/1000)"), '<', 200)
    

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

    0 讨论(0)
提交回复
热议问题