So I'm trying to find the closest points to another particular point within a certain distance, and I'm using the haversine formula for this. The raw query itself is:
SELECT id,
( 3959 * acos( cos( radians(input_lat) ) * cos( radians( map_loc_lat ) ) * cos( radians( map_loc_long ) - radians(input_long) ) + sin( radians(input_lat) ) * sin( radians( map_loc_lat ) ) ) )
AS distance FROM posts HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
Where input_lat
and input_long
are my predefined coordinates that I want my center searching point to begin from, and map_loc_lat
and map_loc_long
are the posts
' points.
I'm trying to use Eloquent's selectRaw()
method to do a portion of the heavy lifting, like so:
$query->selectRaw('id, ( 6371 * acos( cos( radians(?) ) * cos( radians( map_loc_lat ) )
* cos( radians( ? ) - radians(?) ) + sin( radians(?) )
* sin( radians( map_loc_lat ) ) ) ) AS distance', )
But I'm confused on how I should specify the question marks to indicate my input latitude and longitude using Eloquent, plus how would I continue chaining on my order by clause and my limits?