astrogin/laravel-mysql-spatial

MySQL 空间数据类型扩展用于 Laravel。

1.0.0 2017-06-04 21:49 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:11:01 UTC


README

Build Status Code Climate Code Climate Packagist Packagist license

Laravel 包,可轻松使用 MySQL 空间数据类型MySQL 空间函数

请检查您的 MySQL 版本的文档。MySQL 的空间数据扩展自 MySQL 5.5 开始加入,但许多空间函数在 5.6 和 5.7 中有所改变。

安装

使用 composer 添加包

composer require astrogin/laravel-mysql-spatial

config/app.php 中注册服务提供者

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

快速入门

创建迁移

从命令行

php artisan make:migration create_places_table

然后编辑您刚刚创建的迁移,至少添加一个空间数据字段

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');
    }
}

运行迁移

php artisan migrate

创建模型

从命令行

php artisan make:model Place

然后编辑您刚刚创建的模型。它必须使用 SpatialTrait 并定义一个名为 $spatialFields 的数组,其中包含在迁移中创建的 MySQL 空间数据字段名称

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',
    ];
}

保存模型

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

检索模型

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

迁移

可用的 MySQL 空间类型 迁移模板

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

空间索引

您可以使用 spatialIndexdropSpatialIndex 模板在迁移中添加或删除空间索引。

关于空间索引的说明,请参阅 MySQL 文档

对于 MyISAM 和 (自 MySQL 5.7.5 以来) InnoDB 表,MySQL 可以使用类似于创建常规索引的语法创建空间索引,但使用 SPATIAL 关键字。空间索引中的列必须声明为 NOT NULL

从命令行

php artisan make:migration update_places_table

然后编辑您刚刚创建的迁移

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();
        });
    }
}

模型

可用的几何类

  • Point
  • LineString
  • Polygon
  • MultiPoint
  • MultiLineString
  • MultiPolygon
  • GeometryCollection

鸣谢

最初受 njbarrett 的 Laravel postgis 包 的启发。