Perform fast and efficient radius searches on your Laravel Eloquent models.
Perform fast and efficient radius searches on your Laravel Eloquent models.
Laravel Geoly provides a convenient way for your Laravel Eloquent models to query in a certain radius around a position. It is lightning fast by using a bounding box to cut down the possible results and calculating the distance only on the remaining subset. Laravel Geoly works on both MySQL and PostgreSQL.
Simply require the project via composer:
$ composer require akuechler/laravel-geoly
Geoly assumes the two columns latitude
and longitude
on your eloquent model. Simply add them to your migration if not present yet.
$table->double('latitude');
$table->double('longitude');
If you prefer to use other names for your database columns, specify them in your model.
const LATITUDE = 'lat';
const LONGITUDE = 'lng';
Use the Geoly package within your Eloquent model.
class YourModel extends Model
{
use Geoly;
...
}
To search for all models within a specific radius around a position, add the radius
scope to your query.
$query = YourModel::radius($latitude, $longitude, $radius);
$query->get();
This project is heavily inspired by Laravel Geographical and Movable Type Scripts article on Selecting points within a bounding circle.