Thursday, April 12, 2018

Converting Mysql Nearby Query to Laravel API Query - development

Converting Mysql Nearby Query to Laravel API Query

I’m curious about how can I convert this query to Laravel query.

here is my nearby query I tried on phpmyadmin exactly working.

set @lat=37.034126;
set @lng=27.430235;

SELECT party_title, latitude, longitude, 111.045 * DEGREES(ACOS(COS(RADIANS(@lat))
 * COS(RADIANS(latitude))
 * COS(RADIANS(longitude) - RADIANS(@lng))
 + SIN(RADIANS(@lat))
 * SIN(RADIANS(latitude))))
AS distance_in_km
FROM parties
ORDER BY distance_in_km ASC
LIMIT 0,5

query result :

query results

I tried like this but it didnt work.

 public function index(IlluminateHttpRequest $request) {
        if($request->has('party_category')){
           $parties = Parties::where('party_category', $request->party_category)->get();//your new query here
        }
        else if($request->has('lat') && $request->has('long')){
            $parties = Parties::whereRaw("ACOS(SIN(RADIANS('latitude'))*SIN(RADIANS($request->lat))+COS(RADIANS('latitude'))*COS(RADIANS($request->lat))*COS(RADIANS('longitude')-RADIANS($request->long)))")->get();
        }else {
            $parties = Parties::all();
        }
        return Fractal::includes('places')->collection($parties,new PartyTransformer);
     }

I’m curious how can I convert query and how can I get distance_in_km value from query becuase I want to show in api response.



from Laravel Questions and Answers https://laravelquestions.com/php/converting-mysql-nearby-query-to-laravel-api-query/
via Lzo Media

No comments:

Post a Comment