jaejamesdev / laravelcloseto
A simple package that allows you to get the closest record to a given latitude and longitude.
Requires
- php: >=7.1.3
This package is auto-updated.
Last update: 2025-05-29 01:24:45 UTC
README
Laravel CloseTo is a cheeky little eloquent scope to get points within a certain radius area from a given latitude or longitude based off of the Haversine formula.
Installation
$ composer require jaejamesdev/laravelcloseto
Usage
Basic Example:
YourModel::closeTo($latitude, $longitude, $radiusInMiles);
The above example relies on your database having a column called 'latitude' and a column called 'longitude'. Should your database have a naming structure that is different to 'latitude' and 'longitude', no worries at all, you can use the below:
Advanced Example:
YourModel::closeTo($latitude, $longitude, $radiusInMiles, $latiudeColumnName, $longitudeColumnName);
Ordering By Distance:
If you want to get the closest results within a radius ordered by distance, this plugin can take care of that for you in a very simple way!
YourModel::closeTo($latitude, $longitude, $radiusInMiles)->orderBy('distance', 'asc');
Todos
- Add in option to convert between miles and kilometers.
- Improve documentation.
Issues & Requests
See the issues section on Github!
Follow Me
Why not give me a follow on twitter?