1

I tried this on database using SQL tool. It's working perfectly. But Now i Am struggling to Implement it to my DB: query Builder.Any help or suggestions will be grateful for me. If more details need please comment..I will follow next

SELECT u.*, (6371 * acos(cos(radians(22.559648)) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(88.415552)) + sin(radians(22.559648)) * sin(radians(`lat`)))) AS distances 
FROM users AS u
JOIN locations AS l ON `u`.`location_id` = `l`.`id` 
HAVING distances < 32.688888 
ORDER BY distances DESC

1 Answer 1

1

@JinalSomaiya I changed following things and work fine.

 ->having('distances', '<', 32.688888])

to

->having('distances', '<', [32.688888])

and

->join('locations as l', 'l.id', '=', 'users. location_id')

to

->join('locations as l', 'users.location_id', '=', 'l.id')

Final Query:

Edit

DB::table('users')
            ->join('locations as l', 'users.location_id', '=', 'l.id')
            ->select('users.*', DB::raw('(6371 * acos(cos(radians(22.559648)) * cos(radians(`lat`)) * cos(radians(`lng`) - radians(88.415552)) + sin(radians(22.559648)) * sin(radians(`lat`)))) as distances'))
            ->having('distances', '<', 32.688888)
            ->orderBy('distances', 'DESC')
            ->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.