Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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");
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.