wantsJson()) { return response() ->json(Shop::all()); } return abort(404); } /** * Get the 5 nearest shops with the given coords in the request. * * @param App\Http\Requests\Fo\Shop\GetNearestShopRequest $request * @return json */ public function getNearest(GetNearestShopRequest $request) { if (!$request->wantsJson()) { return abort(404); } $latitude = $request->latitude; $longitude = $request->longitude; $raw = DB::raw( 'ROUND ((6371*acos(cos(radians(' . $latitude . '))*cos(radians(latitude))*cos(radians(longitude) - radians(' . $longitude . '))+sin(radians(' . $latitude . '))*sin(radians(latitude)))), 2) AS distance' ); $results = DB::table("shops")->select('*')->addSelect($raw)->orderBy('distance', 'ASC')->limit(5)->get(); return response() ->json($results); } }