Oops!! That was easy!!
$result = DB::select("SELECT id, branchname,lat,lng, distance_in_mi
FROM (
SELECT id, branchname,lat,lng,r,
69.0 * DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(lat))
* COS(RADIANS(longpoint) - RADIANS(lng))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(lat)))) AS distance_in_mi
FROM branches
JOIN (
SELECT '38.226' AS latpoint, '-122.6380' AS longpoint, 200.0 AS r
) AS p
WHERE lat
BETWEEN latpoint - (r / 69)
AND latpoint + (r / 69)
AND lng
BETWEEN longpoint - (r / (69 * COS(RADIANS(latpoint))))
AND longpoint + (r / (69 * COS(RADIANS(latpoint))))
) d
WHERE distance_in_mi <= r
ORDER BY distance_in_mi");
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community