Laravel Mysql Spatial Builder Extension

Overview

magutti-spatial V2

Laravel Builder Mysql Spatial Extension

Latest Stable Version Total Downloads License PHP Version Require

Laravel Builder extensions to calculate distances between two Spatial points using Mysql native function ST_Distance_Sphere.

ST_Distance_Sphere default unit to find distance is meters.

Installation

You can install the package via composer:

composer require magutti/magutti-spatial

Usage

Add in your Model

use Magutti\MaguttiSpatial\Builders\SpatialBuilder;

class Location extends Model
{
    .......
    // you can override the default longitude and latitude fields
    protected $spatialFields = [
        'lng',
        'lat'
    ];
   
    function newEloquentBuilder($query): SpatialBuilder
    {
        return new SpatialBuilder($query);
    }
    .......
}

Example of usage

Get all points where the distance from a given position are less than 1Km

Location::select(['id','lng','lat'])
           ->whitDistance([8.9246844, 45.4152695]) // return distance in meters (default)
           ->whereDistance([8.9246844, 45.4152695],1000)
           ->get()

where 8.9246844 (longitude), 45.4152695 (latitude) is your position and 1000 is the max distance in meters.

SELECT `id`,
       `lng`,
       `lat`,
       St_distance_sphere(Point(8.9246844, 45.4152695), Point(lng, lat)) * 1 AS
       distance
FROM   `locations`
WHERE  St_distance_sphere(Point(8.9246844, 45.4152695), Point(lng, lat)) < 1000 

Using Miles

Location::select(['id','lat','lng'])
           ->whitDistanceInMiles([8.9246844, 45.4152695]) // return distance in Miles
           ->whereDistance([8.9246844, 45.4152695],10,'mi')
           ->get()

Find the closest point to you in Km

Location::select(['id','lat','lng'])
           ->whitDistanceInKm([8.9246844, 45.4152695]) 
           ->whereDistance([8.9246844, 45.4152695],10,'km')
           ->closest()

Helpers

The package provide some pre-built methods to calculate distance in Km, Miles or Feet.

whitDistanceInKm(array $point)    -> return distance in Km;
whitDistanceInMiles(array $point) -> return distance in Miles (mi);
whitDistanceInFeet(array $point)  -> return distance in Feet (ft);

and for filtering by distance

whereDistanceInKm(array $point, float $distance)     -> filter point by a given distance in Km
whereDistanceInMiles(array $point, float $distance)  -> filter point by a given distance in Miles
whereDistanceInFeet(array $point, float $distance)   -> filter point by a given distance in Miles

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Generate and autoload custom Helpers, Builder Scope, Service class, Trait

laravel-make-extender Generate and autoload custom helpers, It can generate multilevel helpers in the context of the directory. Generate Service class

this is cv-builder project in php and ajax

download the project make a database has name cv_builder extract the project in xampp/htdocs/ open the project localhost/cv_builder/index.php or use t

🎨 Free custom elements for the WordPress plugin Oxygen Builder.

🎨 Custom Elements for Oxygen Builder Free custom elements for the WordPress plugin Oxygen Builder. If you find the elements useful, click on the star

 HydePHP - Elegant and Powerful Static App Builder
HydePHP - Elegant and Powerful Static App Builder

HydePHP - Elegant and Powerful Static App Builder Make static websites, blogs, and documentation pages with the tools you already know and love. About

A demo of how to use filament/forms to build a user-facing Form Builder which stores fields in JSON.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

A WPDB wrapper and query builder library.

DB A WPDB wrapper and query builder library. Installation It's recommended that you install DB as a project dependency via Composer: composer require

A Laravel extension for using a laravel application on a multi domain setting
A Laravel extension for using a laravel application on a multi domain setting

Laravel Multi Domain An extension for using Laravel in a multi domain setting Description This package allows a single Laravel installation to work wi

Laravel router extension to easily use Laravel's paginator without the query string

🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨 We don't use this package anymore in our own projects and cannot justify the time needed to maintain it anymore.

dcat laravel-admin extension for china distpicker
dcat laravel-admin extension for china distpicker

dcat-admin extension Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的dcat-admin扩展,用来将Distpicker集成进dcat-admin的表单中. 此扩展参考与引用: dcat-admin-extensions-distpick

Releases(v3.0.0)
Owner
Marco Asperti
LAMP Web developer - Laravel Artisan - Creator of maguttiCms
Marco Asperti
Provides a Eloquent query builder for Laravel or Lumen

This package provides an advanced filter for Laravel or Lumen model based on incoming requets.

M.Fouladgar 484 Jan 4, 2023
Reactive Form Builder for Vue.js with Laravel Support

Dynamic Form Builder for Laravel with Vue.js Create even the most complex forms with ease, using two-sided validation, eloquent, nested elements, cond

Laraform 340 Dec 31, 2022
A TWBS menu builder for Laravel

Laravel Menu Builder A menu builder for Laravel 4-5 using Bootstrap's markup. Документация на Русском Note that this package is shipped with no styles

Alexander Kalnoy 24 Nov 29, 2022
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.

QueryBuilderParser Status Label Status Value Build Insights Code Climate Test Coverage QueryBuilderParser is designed mainly to be used inside Laravel

Tim Groeneveld 149 Nov 11, 2022
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Laravel MongoDB This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library ex

Jens Segers 6.3k Jan 7, 2023
A powerful form builder, for Laravel and other frameworks (stand-alone too)

Former A Laravelish way to create and format forms Former outputs form elements in HTML compatible with your favorite CSS framework (Bootstrap and Fou

null 1.3k Dec 22, 2022
Laravel API architecture builder based on artisan commands.

??‍?? API-Formula Laravel API architecture builder based on artisan commands. This package provides a nice and fluent way to generate combined control

Krševan Lisica 1 Jan 16, 2022
A Laravel Admin Starter project with Page Builder, Roles, Impersonation, Analytics, Blog, News, Banners, FAQ, Testimonials and more

Laravel CMS Starter Project A Laravel CMS Starter project with AdminLTE theme and core features. Preview project here User: [email protected]

Ben-Piet O'Callaghan 306 Nov 28, 2022
Builder - A handful of tools for Rapid Laravel Development

Grafite Builder Grafite has archived this project and no longer supports or develops the code. We recommend using only as a source of ideas for your o

Grafite Inc 997 Dec 22, 2022
A DynamoDB based Eloquent model and Query builder for Laravel.

Laravel DynamoDB A DynamoDB based Eloquent model and Query builder for Laravel. You can find an example implementation in kitar/simplechat. Motivation

Satoshi Kita 146 Jan 2, 2023