jackardios/laravel-eloquent-spatial

Laravel空间库

v2.0.0 2024-01-25 21:19 UTC

This package is auto-updated.

Last update: 2024-09-30 22:00:59 UTC


README

Latest Version on Packagist Tests Static code analysis Lint Total Downloads

此Laravel包允许您轻松地与空间数据类型和函数一起工作。

此包支持MySQL v8、MySQL v5.7和MariaDB v10。

入门指南

安装包

您可以通过Composer安装此包

composer require matanyadaev/laravel-eloquent-spatial

设置第一个模型

  1. 首先,运行以下命令来生成一个新的模型和迁移文件:

    php artisan make:model {modelName} --migration
  2. 接下来,在迁移文件中添加一些空间列。例如,创建一个"places"表:

    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');
        }
    }
  3. 运行迁移

    php artisan migrate
  4. 在您的模型中,填充$fillable$casts数组,并使用HasSpatial特性

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use MatanYadaev\EloquentSpatial\Objects\Point;
    use MatanYadaev\EloquentSpatial\Objects\Polygon;
    use MatanYadaev\EloquentSpatial\Traits\HasSpatial;
    
    /**
     * @property Point $location
     * @property Polygon $area
     */
    class Place extends Model
    {
        use HasSpatial;
    
        protected $fillable = [
            'name',
            'location',
            'area',
        ];
    
        protected $casts = [
            'location' => Point::class,
            'area' => Polygon::class,
        ];
    }

与空间数据交互

设置完模型后,您现在可以创建和访问空间数据。以下是一个示例:

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

// Create new records

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

$whiteHouse = Place::create([
    'name' => 'White House',
    'location' => new Point(38.8976763, -77.0365298, Srid::WGS84->value), // with SRID
]);

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

// Access the data

echo $londonEye->location->latitude; // 51.5032973
echo $londonEye->location->longitude; // -0.1217424

echo $whiteHouse->location->srid; // 4326

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

进一步阅读

有关API的更全面文档,请参阅API页面。

扩展

您可以通过宏向Geometry类添加新方法。

以下是在服务提供者的boot方法中注册宏的示例:

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Geometry::macro('getName', function (): string {
            /** @var Geometry $this */
            return class_basename($this);
        });
    }
}

在您的代码中使用此方法

$londonEyePoint = new Point(-0.1217424, 51.5032973);

echo $londonEyePoint->getName(); // Point

开发

以下是一些有用的开发命令

  • 运行测试:composer pest
  • 运行带覆盖率的测试:composer pest-coverage
  • 执行类型检查:composer phpstan
  • 格式化代码:composer php-cs-fixer

在运行测试之前,请确保运行docker-compose up来启动数据库容器。

更新和更改

有关更新和更改的详细信息,请参阅我们的变更日志

许可证

Laravel Eloquent Spatial在MIT许可证(MIT)下发布。有关更多信息,请参阅我们的许可证文件