Skip to content
Advertisement

Get all profiles in lat/long + range (laravel – laravel-mysql-spatial)

I am trying to build a form in my laravel app to get all users’ profiles that are in or x km around one selected city from my cities table. I’m trying to use the laravel-mysql-spatial that makes use of the ST_DISTANCE_SPHERE mysql function but cant really wrap my brain around the eloquent stuff needed

Basicly each profile is linked to a certain city that has a location POINT() field but i would like to be able to return all profiles that are x km around a city i select via a form.

User model:

JavaScript

Profile model:

JavaScript

City model:

JavaScript

the laravel-mysql-spatial has the following function to calculate distance between 2 points

JavaScript

but cant really figure out how/where to plug it in to get the desired output

JavaScript

this is gets my users/profiles

JavaScript

and im using this to filter by city when selecting a city.

JavaScript

im getting this via a range input as distance

I’ve already looked similar questions up but wasnt able to wire everything together to get the desired output. Any help would be greatly apreciated

Advertisement

Answer

I think you can use whereHas on the profile.city instead of the profile:

JavaScript

This should return all users who have profiles which have a city which is within the given distance.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement