klentroxx / laravel-spatial
Laravel Eloquent 空间数据包
v1.0.5
2024-04-24 19:32 UTC
Requires
- php: ^8.1
- ext-json: *
- ext-pdo: *
- laravel/framework: ^10.0|^11.0
- phayes/geophp: ^1.2
Requires (Dev)
- doctrine/dbal: ^3.0
- laravel/pint: ^1.5
- nunomaduro/larastan: ^1.0|^2.4
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.0|^2.6
- pestphp/pest-plugin-laravel: ^1.0|^2.0
This package is auto-updated.
Last update: 2024-09-24 20:21:20 UTC
README
此 Laravel 包允许您轻松处理空间数据类型和函数。
- v2 支持 Laravel 10+、11+ 和 PHP 8.1+
- v1 支持 Laravel 8、9 和 PHP 8.1+
此包支持 MySQL v8 或 v5.7,以及 MariaDB v10。
入门指南
安装包
您可以通过 composer 安装此包
composer require klentroxx/laravel-spatial
配置
默认配置文件包含几何类型映射
<?php use ASanikovich\LaravelSpatial\Enums\GeometryType; use ASanikovich\LaravelSpatial\Geometry; return [ GeometryType::POINT->value => Geometry\Point::class, GeometryType::POLYGON->value => Geometry\Polygon::class, /// ... ];
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-spatial-config"
如果您想,您可以覆盖自定义几何类型映射
- 全局配置文件
- 通过模型中的自定义
$casts
(优先级最高)
设置您的第一个模型
-
首先,通过运行以下命令生成新的模型和迁移文件
php artisan make:model {modelName} --migration
-
接下来,将一些空间列添加到迁移文件中。例如,要创建一个 "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'); } }
-
运行迁移
php artisan migrate
-
在您的模型中,填充
$casts
数组并使用HasSpatial
特性(填充$fillable
- 可选)namespace App\Models; use Illuminate\Database\Eloquent\Model; use ASanikovich\LaravelSpatial\Eloquent\HasSpatial; use ASanikovich\LaravelSpatial\Geometry\Point; use ASanikovich\LaravelSpatial\Geometry\Polygon; /** * @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 ASanikovich\LaravelSpatial\Geometry\Polygon; use ASanikovich\LaravelSpatial\Geometry\LineString; use ASanikovich\LaravelSpatial\Geometry\Point; use ASanikovich\LaravelSpatial\Enums\Srid; // Create new records $londonEye = Place::create([ 'name' => 'London Eye', 'location' => new Point(51.5032973, -0.1217424), ]); $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(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), ]), ]), ]) // 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":[[[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 的更全面文档,请参阅 API 页面。
仅使用作用域方法创建查询
Place::whereDistance(...); // This is IDE-friendly
扩展
您可以通过宏向 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(51.5032973, -0.1217424); echo $londonEyePoint->getName(); // Point
开发
以下是一些有用的开发命令
在运行测试之前,通过 docker-compose 运行 db
docker-compose up -d
运行测试
composer run test
运行带有覆盖率的测试
composer run test-coverage
执行类型检查
composer run phpstan
格式化代码
composer run format
更新和更改
有关更新和更改的详细信息,请参阅我们的 变更日志。
许可
Laravel Spatial 根据 MIT 许可证(MIT)发布。有关更多信息,请参阅我们的 许可证文件。
鸣谢
最初受到 MatanYadaev 的 laravel-eloquent-spatial 包 的启发。此包是 Asanikovich 的 laravel-spatial 包 的分支