I'm using Laravel 5.2 and have a sql query using the query builder functions and have broken a query when switching from a MySQL database to PostgreSQL. The query is meant to get all locations within a certain number of miles of a latitude and longitude.
$locations = Location::select(
DB::raw("*,
( 3959 * acos( cos( radians(?) ) *
cos( radians( lat) )
* cos( radians( long ) - radians(?)
) + sin( radians(?) ) *
sin( radians( lat ) ) )
) AS distance"))
->having("distance", "<", $distance)
->orderBy("distance")
->setBindings([$latitude, $longitude, $latitude])
->get();
Once switching to the PostgreSQL data source, I get this error:
QLSTATE[42703]: Undefined column: 7 ERROR: column "distance" does not exist
LINE 7: ) AS distance from "locations" having "distance" < $4 o...
^ (SQL: select *,
( 3959 * acos( cos( radians(37.7959362) ) *
cos( radians( lat ) )
* cos( radians( long ) - radians(-122.4000032)
) + sin( radians(37.7959362) ) *
sin( radians( lat ) ) )
) AS distance from "locations" having "distance" < 15 order by "distance" asc)
What would be the best way to fix this problem. I know it's a difference in the SQL syntax between MySQL and Postgres that is created by Laravel but I'm tearing my hair out trying to figure out how to fix this error.
Any help would be greatly appreciated!