How to get nearby courts from given lat long in laravel?

Viewed 3581

I want to get basketball courts on basis of given lat long in eloquent ORM. I tried to get that but here I got this error.

Call to undefined function App\Http\Controllers\Api\RADIANS()

is there any way to get nearby location using Eloquent if yes then how? please help me out I'm sharing me doings so far.

HomeCourt Controller

public function fetchHomeCourts(Request $request) {

    $validator = Validator::make(
        array(
            'lat' => $request->lat,
            'long' => $request->long,
        ),
        array(
            'lat' => 'required',
            'long' => 'required',
        )
    );
    if ($validator->fails()) {
        $this->setMeta("422", Constant::MSG_422);
        return response()->json($this->setResponse());
    }
    $homeCourt= HomeCourt::where(ACOS(SIN(RADIANS('latitude'))*SIN(RADIANS($request->lat))+COS(RADIANS('latitude'))*COS(RADIANS($request->lat))*COS(RADIANS('longitude')-RADIANS($request->long)))*6380 < 10)->first();
    if(!$homeCourt) {
        $this->setMeta("200", "No courts found");
        $this->setData("homeCourts",$homeCourt);
        return response()->json($this->setResponse());
    }
    $this->setMeta("200", "Home court list");
    $this->setData("homeCourts",$homeCourt);
    return response()->json($this->setResponse());

}
3 Answers

I have tried the above answer with no success, but thanks to @apokryfos answer i did this, so, this is my code, i put it in a raw DB query.

\DB::table("users")
     ->select("users.id", \DB::raw("6371 * acos(cos(radians(" . $this->lat . "))
     * cos(radians(users.latitude)) 
     * cos(radians(users.longitude) - radians(" . $this->lng . ")) 
     + sin(radians(" .$this->lat. ")) 
     * sin(radians(users.latitude))) AS distance"))
     ->having('distance', '<', $this->rad)
     ->first();
Related