PHPackages                             astrogin/laravel-mysql-spatial - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. astrogin/laravel-mysql-spatial

ActiveLibrary[Database &amp; ORM](/categories/database)

astrogin/laravel-mysql-spatial
==============================

MySQL spatial data types extension for Laravel.

1.0.0(8y ago)11.5kMITPHPPHP &gt;=5.5

Since Jun 4Pushed 8y ago1 watchersCompare

[ Source](https://github.com/astrogin/laravel-mysql-spatial)[ Packagist](https://packagist.org/packages/astrogin/laravel-mysql-spatial)[ RSS](/packages/astrogin-laravel-mysql-spatial/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (8)Versions (4)Used By (0)

Laravel MySQL Spatial extension
===============================

[](#laravel-mysql-spatial-extension)

[![Build Status](https://camo.githubusercontent.com/2779e6e69c69320c8e3c4194b007e7e055f3f4cecd4bd214d139777c9477ba18/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6772696d7a792f6c61726176656c2d6d7973716c2d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/grimzy/laravel-mysql-spatial)[![Code Climate](https://camo.githubusercontent.com/94e48b24ce45113ec03f765df3ac21c5668dd26ecaa95376206ea5eaf67f84f3/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f6772696d7a792f6c61726176656c2d6d7973716c2d7370617469616c2f6261646765732f6770612e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/grimzy/laravel-mysql-spatial)[![Code Climate](https://camo.githubusercontent.com/c0439a991d1c81d98e39b6a51e26eb55a9df02e36e446296bf27e63d45d90bee/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f636f7665726167652f6769746875622f6772696d7a792f6c61726176656c2d6d7973716c2d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/grimzy/laravel-mysql-spatial/coverage)[![Packagist](https://camo.githubusercontent.com/f45c7720c76f541083b28884bd78620436cbd62798b03297f3d2db2d0357fe5f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6772696d7a792f6c61726176656c2d6d7973716c2d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/grimzy/laravel-mysql-spatial)[![Packagist](https://camo.githubusercontent.com/ca275852df946c456ff6f008000508c88bd44f92984dfc9bf533be89e0cb7f5a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6772696d7a792f6c61726176656c2d6d7973716c2d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/grimzy/laravel-mysql-spatial)[![license](https://camo.githubusercontent.com/7123c32787e013be5a8a13598ad01f562754637ed6141e89b02e85bf16d3e63e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6173686170652f6170697374617475732e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Laravel package to easily work with [MySQL Spatial Data Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) and [MySQL Spatial Functions](https://dev.mysql.com/doc/refman/5.7/en/spatial-function-reference.html).

Please check the documentation for your MySQL version. MySQL's Extension for Spatial Data was added in MySQL 5.5 but many Spatial Functions were changed in 5.6 and 5.7.

Installation
------------

[](#installation)

Add the package using composer:

```
composer require astrogin/laravel-mysql-spatial
```

Register the service provider in `config/app.php`:

```
'providers' => [
  /*
   * Package Service Providers...
   */
  Grimzy\LaravelMysqlSpatial\SpatialServiceProvider::class,
],
```

Quickstart
----------

[](#quickstart)

### Create a migration

[](#create-a-migration)

From the command line:

```
php artisan make:migration create_places_table
```

Then edit the migration you just created by adding at least one spatial data field:

```
use Illuminate\Database\Migrations\Migration;
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;

class CreatePlacesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('places', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name')->unique();
            // Add a Point spatial data field named location
            $table->point('location')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('places');
    }
}
```

Run the migration:

```
php artisan migrate
```

### Create a model

[](#create-a-model)

From the command line:

```
php artisan make:model Place
```

Then edit the model you just created. It must use the `SpatialTrait` and define an array called `$spatialFields` with the name of the MySQL Spatial Data field(s) created in the migration:

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

/**
 * @property \Grimzy\LaravelMysqlSpatial\Types\Point $location
 */
class Place extends Model
{
    use SpatialTrait;

    protected $fillable = [
        'name',
    ];

    protected $spatialFields = [
        'location',
    ];
}
```

### Saving a model

[](#saving-a-model)

```
$place1 = new Place();
$place1->name = 'Empire State Building';
$place1->location = new Point(40.7484404, -73.9878441);
$place1->save();
```

### Retrieving a model

[](#retrieving-a-model)

```
$place2 = Place::first();
$lat = $place2->location->getLat();	// 40.7484404
$lng = $place2->location->getLng();	// -73.9878441
```

Migration
---------

[](#migration)

Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:

- geometry
- point
- lineString
- polygon
- multiPoint
- multiLineString
- multiPolygon
- geometryCollection

### Spatial index

[](#spatial-index)

You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.

Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):

> For [`MyISAM`](https://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html) and (as of MySQL 5.7.5) `InnoDB` tables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using the `SPATIAL` keyword. Columns in spatial indexes must be declared `NOT NULL`.

From the command line:

```
php artisan make:migration update_places_table
```

Then edit the migration you just created:

```
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdatePlacesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // MySQL < 5.7.5: table has to be MyISAM
        // \DB::statement('ALTER TABLE places ENGINE = MyISAM');

        Schema::table('places', function (Blueprint $table) {
            // Make sure point is not nullable
            $table->point('location')->change();

            // Add a spatial index on the location field
            $table->spatialIndex('location');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('places', function (Blueprint $table) {
            $table->dropSpatialIndex(['location']); // either an array of column names or the index name
        });

        // \DB::statement('ALTER TABLE places ENGINE = InnoDB');

        Schema::table('places', function (Blueprint $table) {
            $table->point('location')->nullable()->change();
        });
    }
}
```

Models
------

[](#models)

Available geometry classes:

- Point
- LineString
- Polygon
- MultiPoint
- MultiLineString
- MultiPolygon
- GeometryCollection

Credits
-------

[](#credits)

Originally inspired from [njbarrett's Laravel postgis package](https://github.com/njbarrett/laravel-postgis).

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 90.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3263d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ba9ca8e9c6e041adec2c58d4e8b8fd54cb50a9372ec9328b29f42f98b39f087?d=identicon)[astrogin](/maintainers/astrogin)

---

Top Contributors

[![grimzy](https://avatars.githubusercontent.com/u/1837678?v=4)](https://github.com/grimzy "grimzy (28 commits)")[![astrogin](https://avatars.githubusercontent.com/u/13066161?v=4)](https://github.com/astrogin "astrogin (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/astrogin-laravel-mysql-spatial/health.svg)

```
[![Health](https://phpackages.com/badges/astrogin-laravel-mysql-spatial/health.svg)](https://phpackages.com/packages/astrogin-laravel-mysql-spatial)
```

###  Alternatives

[limenet/laravel-mysql-spatial

MySQL spatial data types extension for Laravel.

1214.2k](/packages/limenet-laravel-mysql-spatial)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
