Wednesday, March 15, 2017

How to query distances between two coordinates with Eloquent

I know this has been ask many times. But I didn't figure out to make it according to my needs.

I need to query the nearest users from another user. Basically, I have a users table this table has a one to one relation with the users_locations table which has a latitude and a longitude field.

So I've seen this https://laravel.io/forum/04-23-2014-convert-this-geolocation-query-to-query-builder-for?page=1 and this may be the best solution.

But my basic query is :

\App\Model\User::whereNotIn('id', $ids)
               ->where('status', 1)
               ->whereHas('user_location', function($q) use ($lat, $lng, $radius) {
                    /** This is where I'm stuck to write the query **/
             })->select('id', 'firstname')
               ->get();

I don't figure out how to implement the solution in this case.

Thank you in advance for your help



via KeizerBridge

Advertisement