Convert SQL query to Eloquent Laravel based on two parameters

- 1 answer

Ad

I have a sql query:

SELECT lat, lng, SQRT(
        POW(69.1 * (lat - $lat), 2) +
        POW(69.1 * ($lng - lng) * COS(lat / 57.3), 2)) AS distance
FROM locations HAVING distance < 25 ORDER BY distance;

Now I want to convert it to Eloquent But here is the problem
TIll now, I have queried my tables based on one parameter
Venue::where('lat',">",12.133) But Now I wanna kinda run a procedure on my table And I don't know How to accomplish this kind of queries.
How Do yo do that?

Ad

Answer

Ad

I really doubt you can get that query using Eloquent... But you can use query builder and raw:

Locations::select(DB::raw("
        lat,lng, SQRT(
            POW(69.1 * (lat - $lat), 2) +
            POW(69.1 * ($lng - lng) * COS(lat / 57.3), 2)) AS distance"))
->havingRaw('distance < 25')
->orderBy('distance')
Ad
source: stackoverflow.com
Ad