danhunsaker / laravel-spatial
Spatial data types extension for Laravel.
Fund package maintenance!
danhunsaker
Patreon
Ko Fi
Liberapay
paypal.me/hunsakerdan
Installs: 8 961
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 4
Open Issues: 1
Requires
- php: ^7.2
- doctrine/dbal: ^2.5
- funiq/geophp: dev-master
- illuminate/database: ^5.7 || ^6.0 || ^7.0
- jmikola/geojson: ^1.0
Requires (Dev)
- laravel/laravel: ^7.0
- mockery/mockery: 1.3.*
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-10-20 13:14:37 UTC
README
This package is fully undocumented and unstable, and is a combination of the two great packages:
Installation
Installation made super-easy with composer:
composer require danhunsaker/laravel-spatial
Requirements
Works with PostgreSQL installed PostGIS extension and MySQL at least version 5.6.
If you try using it on a shared host which is not fulfilling those requirements, change your provider.
Usage
Migrations
Add spatial fields to your migrations the same way you would any others:
$table->point('point_column'); $table->linestring('line_string_column'); $table->polygon('polygon_column'); $table->geometry('geometry_column'); $table->multipoint('multi_point_column'); $table->multilinestring('multi_line_string_column'); $table->multipolygon('multi_polygon_column'); $table->geometrycollection('geometry_collection_column');
Models
Any models that use spatial fields need to use the LaravelSpatial\Eloquent\SpatialTrait
, and list the spatial fields themselves in the $spatialFields
property:
use LaravelSpatial\Eloquent\SpatialTrait; // ... class MyModel extends Model { use SpatialTrait; // ... protected $spatialFields = ['location']; // ... }
Values
We use the GeoJson PHP Library for describing spatial fields as GeoJSON object, e.g.:
use GeoJSON\Geometry\Point; // ... $eloquent = new MyModel(); $eloquent->location = new Point([49.7, 6.9]); // ... $eloquent->save();