How to Convert search nearby places query to Laravel eloquent ORM?

Issue

This Content is from Stack Overflow. Question asked by kumari shwetha

I am using autocomplete mapbox search places option to search nearby places. I have sql query, it executes correct. But I want to convert exact SQL query into Laravel eloquent. I tried but caught with an error: A non-numeric value encountered

SQL query:

SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( 12.91 - latitude) *  pi()/180 / 2), 2) +COS( 12.91 * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( 74.85 - longitude) * pi()/180 / 2), 2) ))) 
as distance from vehicles
having  distance <= 10 
order by distance

I want to use exact query in controller

In Laravel used same query

$vehicles = $vehicles->select('*',(3956 * 2 * ASIN(SQRT( POW(SIN(( $request->location_lat - 'latitude') *  pi()/180 / 2), 2) +COS( $request->location_lat * pi()/180) * COS('latitude' * pi()/180) * POW(SIN(( $request->location_long - 'longitude') * pi()/180 / 2), 2) ))))
           ->having(distance <= 10)
           ->orderBy(distance,'ASC')
           ->get(); 

How it can be done?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?