PHPackages                             veronj/laravel-eloquent-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. veronj/laravel-eloquent-spatial

ActiveLibrary

veronj/laravel-eloquent-spatial
===============================

Spatial library for Laravel

1.0.4(4y ago)05MITPHPPHP ^8.0

Since Feb 23Pushed 3y agoCompare

[ Source](https://github.com/veronj/laravel-eloquent-spatial)[ Packagist](https://packagist.org/packages/veronj/laravel-eloquent-spatial)[ Docs](https://github.com/matanyadaev/laravel-eloquent-spatial)[ RSS](/packages/veronj-laravel-eloquent-spatial/feed)WikiDiscussions master Synced 1mo ago

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

Laravel Eloquent Spatial
========================

[](#laravel-eloquent-spatial)

[![Latest Version on Packagist](https://camo.githubusercontent.com/fcb4033b6a50bfdbfab2a55f9525fa9a596cef7774bcce9a5b45e42921a0f8b3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6174616e7961646165762f6c61726176656c2d656c6f7175656e742d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial)[![Tests](https://camo.githubusercontent.com/3c4f277bc98d112e489502092064cbf94e322cb5de4585566285666f8b319dfc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d6174616e7961646165762f6c61726176656c2d656c6f7175656e742d7370617469616c2f54657374733f6c6162656c3d7465737473)](https://camo.githubusercontent.com/3c4f277bc98d112e489502092064cbf94e322cb5de4585566285666f8b319dfc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d6174616e7961646165762f6c61726176656c2d656c6f7175656e742d7370617469616c2f54657374733f6c6162656c3d7465737473)[![Static code analysis](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Static%20code%20analysis/badge.svg)](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Static%20code%20analysis/badge.svg)[![Code quality analysis](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Code%20quality%20analysis/badge.svg)](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Code%20quality%20analysis/badge.svg)[![Lint](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Lint/badge.svg)](https://github.com/matanyadaev/laravel-eloquent-spatial/workflows/Lint/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/cf480f9f918a93034e310fb025fdf388ee30464c61cdf5e9a1751cae9ea4f88e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6174616e7961646165762f6c61726176656c2d656c6f7175656e742d7370617469616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial)

Laravel package to work with spatial data types and functions.

This package supports MySQL 5.7 &amp; 8.0 and works on PHP 8 &amp; Laravel 8.

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

[](#installation)

You can install the package via composer:

```
composer require matanyadaev/laravel-eloquent-spatial
```

Quickstart
----------

[](#quickstart)

Generate a new model with a migration file:

```
php artisan make:model {modelName} --migration
```

Add some spatial columns to the migration file:

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

class CreatePlacesTable extends Migration
{
    public function up(): void
    {
        Schema::create('places', static function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->point('location')->nullable();
            $table->polygon('area')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('places');
    }
}
```

Run the migration:

```
php artisan migrate
```

Fill the `$fillable` and `$casts` arrays and add custom eloquent builder to your new model:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MatanYadaev\EloquentSpatial\SpatialBuilder;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;

/**
 * @property Point $location
 * @property Polygon $area
 * @method static SpatialBuilder query()
 */
class Place extends Model
{
    protected $fillable = [
        'name',
        'location',
        'area',
    ];

    protected $casts = [
        'location' => Point::class,
        'area' => Polygon::class,
    ];

    public function newEloquentBuilder($query): SpatialBuilder
    {
        return new SpatialBuilder($query);
    }
}
```

Access spatial data:

```
use App\Models\Place;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;

$londonEye = Place::create([
    'name' => 'London Eye',
    'location' => new Point(51.5032973, -0.1195537)
]);

$vaticanCity = Place::create([
    'name' => 'Vatican City',
    'area' => new Polygon([
        new LineString([
              new Point(12.455363273620605, 41.90746728266806),
              new Point(12.450309991836548, 41.906636872349075),
              new Point(12.445632219314575, 41.90197359839437),
              new Point(12.447413206100464, 41.90027269624499),
              new Point(12.457906007766724, 41.90000118654431),
              new Point(12.458517551422117, 41.90281205461268),
              new Point(12.457584142684937, 41.903107507989986),
              new Point(12.457734346389769, 41.905918239316286),
              new Point(12.45572805404663, 41.90637337450963),
              new Point(12.455363273620605, 41.90746728266806),
        ])
    ])
])
```

Retrieve a record with spatial data:

```
echo $londonEye->location->latitude; // 51.5032973
echo $londonEye->location->longitude; // -0.1195537

echo $vacationCity->area->toJson(); // {"type":"Polygon","coordinates":[[[41.90746728266806,12.455363273620605],[41.906636872349075,12.450309991836548],[41.90197359839437,12.445632219314575],[41.90027269624499,12.447413206100464],[41.90000118654431,12.457906007766724],[41.90281205461268,12.458517551422117],[41.903107507989986,12.457584142684937],[41.905918239316286,12.457734346389769],[41.90637337450963,12.45572805404663],[41.90746728266806,12.455363273620605]]]}
```

API
---

[](#api)

Please see [API](API.md) for more informative API documentation.

Tip for better IDE support
--------------------------

[](#tip-for-better-ide-support)

In order to get better IDE support, you should add a `query` method phpDoc annotation to your model:

```
/**
 * @method static SpatialBuilder query()
 */
class Place extends Model
{
    // ...
}
```

Or alternatively override the method:

```
class Place extends Model
{
    public static function query(): SpatialBuilder
    {
        return parent::query();
    }
}
```

Create queries only with the `query()` static method:

```
Place::query()->whereDistance(...); // This is IDE-friendly
Place::whereDistance(...); // This is not
```

Tests
-----

[](#tests)

```
composer phpunit
# or with coverage
composer phpunit-coverage
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 97% 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

Every ~92 days

Total

5

Last Release

1535d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/584894fa1770db67950c003cc050b9dd88ac878ac23b08b1607108713daa00d6?d=identicon)[veronj](/maintainers/veronj)

---

Top Contributors

[![MatanYadaev](https://avatars.githubusercontent.com/u/13586343?v=4)](https://github.com/MatanYadaev "MatanYadaev (129 commits)")[![veronj](https://avatars.githubusercontent.com/u/16002123?v=4)](https://github.com/veronj "veronj (3 commits)")[![eschricker](https://avatars.githubusercontent.com/u/56429442?v=4)](https://github.com/eschricker "eschricker (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/veronj-laravel-eloquent-spatial/health.svg)

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

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)

PHPackages © 2026

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