|
| 1 | +# Laravel Eloquent Spatial |
| 2 | + |
| 3 | +[](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial) |
| 4 | + |
| 5 | + |
| 6 | +[](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial) |
| 7 | + |
| 8 | +Laravel package to easily work with [MySQL Spatial Data Types](https://dev.mysql.com/doc/refman/8.0/en/spatial-type-overview.html) and [MySQL Spatial Functions](https://dev.mysql.com/doc/refman/8.0/en/spatial-function-reference.html). |
| 9 | + |
| 10 | +This package supports MySQL 5.7 & 8.0. The package works on PHP 8 & Laravel 8 only. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +You can install the package via composer: |
| 15 | + |
| 16 | +```bash |
| 17 | +composer require matanyadaev/laravel-eloquent-spatial |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | +Generate a new model with a migration file: |
| 22 | +```bash |
| 23 | +php artisan make:model --migration |
| 24 | +``` |
| 25 | + |
| 26 | +Add some spatial columns to the migration file: |
| 27 | + |
| 28 | +```php |
| 29 | +use Illuminate\Database\Migrations\Migration; |
| 30 | +use Illuminate\Database\Schema\Blueprint; |
| 31 | + |
| 32 | +class CreatePlacesTable extends Migration |
| 33 | +{ |
| 34 | + public function up(): void |
| 35 | + { |
| 36 | + Schema::create('places', static function (Blueprint $table) { |
| 37 | + $table->id(); |
| 38 | + $table->string('name')->unique(); |
| 39 | + $table->point('location')->nullable(); |
| 40 | + $table->polygon('area')->nullable(); |
| 41 | + $table->timestamps(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + public function down(): void |
| 46 | + { |
| 47 | + Schema::dropIfExists('places'); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Run the migration: |
| 53 | + |
| 54 | +```bash |
| 55 | +php artisan migrate |
| 56 | +``` |
| 57 | + |
| 58 | +Fill the `$fillable` and `$casts` arrays and add custom eloquent builder to your new model: |
| 59 | + |
| 60 | +```php |
| 61 | +namespace App\Models; |
| 62 | + |
| 63 | +use Illuminate\Database\Eloquent\Model; |
| 64 | +use MatanYadaev\EloquentSpatial\SpatialBuilder; |
| 65 | +use MatanYadaev\EloquentSpatial\Objects\Point; |
| 66 | +use MatanYadaev\EloquentSpatial\Objects\Polygon; |
| 67 | + |
| 68 | +/** |
| 69 | + * @property Point $location |
| 70 | + * @property Polygon $area |
| 71 | + * @method static SpatialBuilder query() |
| 72 | + */ |
| 73 | +class Place extends Model |
| 74 | +{ |
| 75 | + protected $fillable = [ |
| 76 | + 'name' |
| 77 | + 'location', |
| 78 | + 'area', |
| 79 | + ]; |
| 80 | + |
| 81 | + protected $spatialFields = [ |
| 82 | + 'location' => Point::class, |
| 83 | + 'area' => Polygon::class, |
| 84 | + ]; |
| 85 | + |
| 86 | + public function newEloquentBuilder($query): SpatialBuilder |
| 87 | + { |
| 88 | + return new SpatialBuilder($query); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Store a record with spatial data: |
| 94 | + |
| 95 | +```php |
| 96 | +use App\Models\Place; |
| 97 | +use MatanYadaev\EloquentSpatial\Objects\Polygon; |
| 98 | +use MatanYadaev\EloquentSpatial\Objects\LineString; |
| 99 | +use MatanYadaev\EloquentSpatial\Objects\Point; |
| 100 | + |
| 101 | +$londonEye = Place::create([ |
| 102 | + 'name' => 'London Eye', |
| 103 | + 'location' => new Point(51.5032973, -0.1195537) |
| 104 | +]); |
| 105 | + |
| 106 | +$vaticanCity = Place::create([ |
| 107 | + 'name' => 'Vatican City', |
| 108 | + 'area' => new Polygon([ |
| 109 | + new LineString([ |
| 110 | + new Point(12.455363273620605, 41.90746728266806), |
| 111 | + new Point(12.450309991836548, 41.906636872349075), |
| 112 | + new Point(12.445632219314575, 41.90197359839437), |
| 113 | + new Point(12.447413206100464, 41.90027269624499), |
| 114 | + new Point(12.457906007766724, 41.90000118654431), |
| 115 | + new Point(12.458517551422117, 41.90281205461268), |
| 116 | + new Point(12.457584142684937, 41.903107507989986), |
| 117 | + new Point(12.457734346389769, 41.905918239316286), |
| 118 | + new Point(12.45572805404663, 41.90637337450963), |
| 119 | + new Point(12.455363273620605, 41.90746728266806), |
| 120 | + ]) |
| 121 | + ]) |
| 122 | +]) |
| 123 | +``` |
| 124 | + |
| 125 | +Retrieve a record with spatial data: |
| 126 | + |
| 127 | +```php |
| 128 | +echo $londonEye->location->latitude; // 51.5032973 |
| 129 | +echo $londonEye->location->longitude; // -0.1195537 |
| 130 | +``` |
| 131 | + |
| 132 | +## Geometry classes |
| 133 | + |
| 134 | +## Available functions |
| 135 | + |
| 136 | +## Tests |
| 137 | + |
| 138 | +``` bash |
| 139 | +composer phpunit |
| 140 | +# or with coverage |
| 141 | +composer phpunit-cover |
| 142 | +``` |
| 143 | + |
| 144 | +## Changelog |
| 145 | + |
| 146 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 147 | + |
| 148 | +## License |
| 149 | + |
| 150 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments