astrogin/laravel-mysql-spatial
Composer 安装命令:
composer require astrogin/laravel-mysql-spatial
包简介
MySQL spatial data types extension for Laravel.
README 文档
README
Laravel package to easily work with MySQL Spatial Data Types and MySQL Spatial Functions.
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
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
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
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
$place1 = new Place(); $place1->name = 'Empire State Building'; $place1->location = new Point(40.7484404, -73.9878441); $place1->save();
Retrieving a model
$place2 = Place::first(); $lat = $place2->location->getLat(); // 40.7484404 $lng = $place2->location->getLng(); // -73.9878441
Migration
Available MySQL Spatial Types migration blueprints:
- geometry
- point
- lineString
- polygon
- multiPoint
- multiLineString
- multiPolygon
- geometryCollection
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:
For
MyISAMand (as of MySQL 5.7.5)InnoDBtables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using theSPATIALkeyword. Columns in spatial indexes must be declaredNOT 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
Available geometry classes:
- Point
- LineString
- Polygon
- MultiPoint
- MultiLineString
- MultiPolygon
- GeometryCollection
Credits
Originally inspired from njbarrett's Laravel postgis package.
astrogin/laravel-mysql-spatial 适用场景与选型建议
astrogin/laravel-mysql-spatial 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.49k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 astrogin/laravel-mysql-spatial 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 astrogin/laravel-mysql-spatial 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-28