akuechler/laravel-geoly

Perform fast and efficient radius searches on your Laravel Eloquent models.

v1.0.6 2021-04-20 07:17 UTC

This package is auto-updated.

Last update: 2024-04-20 14:00:15 UTC


README

Packagist Version PHP from Packagist StyleCI GitHub

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.

Requirements

  • PHP 7.1+
  • Laravel 5+
  • Tested on MySQL and PostgreSQL

Installation

Simply require the project via composer:

$ composer require akuechler/laravel-geoly

How to use

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();

Credits

This project is heavily inspired by Laravel Geographical and Movable Type Scripts article on Selecting points within a bounding circle.