Search code examples
phpmysqllaravelsearchgeo

Laravel 5.2 search for users by distance from users postcode


I have a question regarding Laravel 5.2 and trying to search for users by distance.

So far this is my database layout.

users - the user table holds no personal info.

users_info - this table has the users information (postcode and address) and is connected via same ids (user_id > id)

I also have a table with postcode info called postcodes_geo which has the fields

id int(11)
postcode varchar(5)
suburb varchar(100)
state varchar(4)
latitude decimal(6,3)
longitude decimal(6,3)

Now I am so lost as how I would go about searching for a user by distance from the users postcode who is searching. I am new to PHP as I am just a frontend developer.

Has anyone else set this type of search up before or might be able to give me a hint?


Solution

  • You can use this package: https://github.com/alexpechkarev/google-maps Using this, you can calculate the distance with the postal code or address.

    If you are always calculating the distance relative to a fixed place, try to save the distance as well in the table when you create the user information.

    Now if you need to dynamically calculate the distance, I would first pick all the users within the same suburb (if you need to get the users with the shortest distance), and then use the above package to calculate the distance using their postal code or address.

    This is just an idea. But this would work. You can further give more parameters to get the user collection.