Haversine and Laravel

后端 未结 2 1395
春和景丽
春和景丽 2020-12-20 00:39

I\'m attempting to compare a users location (Sent via params in the URL) to offers.offer_lat and offers.offer_long (in my DB) by a distance argument (set in miles)

I

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 01:04

    Haversine in Laravel works in this way:

                    Travel::select(
                        DB::raw("travels.*,
                              ( 6371 * acos( cos( radians($lat) ) *
                                cos( radians( lat ) )
                                * cos( radians( lon ) - radians($lng)
                                ) + sin( radians($lat) ) *
                                sin( radians( lat ) ) )
                              ) AS distance"))
                    ->orderBy('distance', 'asc')
                    ->get();
    

    and you will get a collection of points ordered by distance (nearest first)

    Was used travel model with params lat and lng. parameter distance is added by the raw query

提交回复
热议问题