All Projects → MatanYadaev → laravel-eloquent-spatial

MatanYadaev / laravel-eloquent-spatial

Licence: MIT license
Laravel Eloquent spatial package.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-eloquent-spatial

R-Geospatial-Fundamentals
This is the repository for D-Lab's Geospatial Fundamentals in R with sf workshop.
Stars: ✭ 42 (-53.33%)
Mutual labels:  spatial
eloquent-has-by-join
Convert has() and whereHas() constraints to join() ones for single-result relations.
Stars: ✭ 21 (-76.67%)
Mutual labels:  eloquent
LaraPersonate
Login as a different user quickly
Stars: ✭ 121 (+34.44%)
Mutual labels:  eloquent
query-filter
Define filters for your Eloquent models based on your request
Stars: ✭ 20 (-77.78%)
Mutual labels:  eloquent
SlimREST
An app skeleton for building a REST API with the Slim PHP Micro-Framework
Stars: ✭ 22 (-75.56%)
Mutual labels:  eloquent
geojson
GeoJSON classes for R
Stars: ✭ 32 (-64.44%)
Mutual labels:  spatial
dilovel
An advanced framework is written in PHP, a framework containing rich components such as middleware, orm, request management, template engine, elasticsearch, template engine, many modern frameworks have been written by adopting clean code principles completely written in accordance with PHP standards. like linux operating system ...All of control…
Stars: ✭ 38 (-57.78%)
Mutual labels:  eloquent
pimpable
No description or website provided.
Stars: ✭ 102 (+13.33%)
Mutual labels:  eloquent
ows4R
R Interface for OGC Web-Services (OWS)
Stars: ✭ 29 (-67.78%)
Mutual labels:  spatial
laravel-tmdb
Interact with TMDB data in your Laravel application.
Stars: ✭ 25 (-72.22%)
Mutual labels:  eloquent
bookvis
Sources of the book "Displaying time series, spatial and space-time data with R" (2nd Edition)
Stars: ✭ 52 (-42.22%)
Mutual labels:  spatial
r-spatial.org
r-spatial.org blog sources
Stars: ✭ 51 (-43.33%)
Mutual labels:  spatial
delaunator-rs
Fast 2D Delaunay triangulation in Rust. A port of Delaunator.
Stars: ✭ 115 (+27.78%)
Mutual labels:  spatial
eloquent-repository
Repository pattern for Eloquent ORM with focus in cache.
Stars: ✭ 30 (-66.67%)
Mutual labels:  eloquent
laravel-route-model-autobinding
THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.
Stars: ✭ 14 (-84.44%)
Mutual labels:  eloquent
laravel-arangodb
ArangoDB driver for Laravel
Stars: ✭ 43 (-52.22%)
Mutual labels:  eloquent
geometa
R tools to write, read & validate geographic metadata (OGC/ISO 19110, 19115, 19119, 19136 and 19139)
Stars: ✭ 37 (-58.89%)
Mutual labels:  spatial
laravel-local-class-scope
A tiny macro that reuse a global scope class as a local scope
Stars: ✭ 16 (-82.22%)
Mutual labels:  eloquent
laravel-searchzy
Package que te permite buscar y filtrar registros de Eloquent Models en Laravel de una manera simple y sencilla.
Stars: ✭ 15 (-83.33%)
Mutual labels:  eloquent
laravel-mass-update
Update multiple Laravel Model records, each with its own set of values, sending a single query to your database!
Stars: ✭ 65 (-27.78%)
Mutual labels:  eloquent

Laravel Eloquent Spatial

Latest Version on Packagist Tests Static code analysis Code quality analysis Lint Total Downloads

Laravel package to work with spatial data types and functions.

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

Installation

You can install the package via composer:

composer require matanyadaev/laravel-eloquent-spatial

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

Please see API for more informative API documentation.

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

composer phpunit
# or with coverage
composer phpunit-coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].