You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel package to easily work with [MySQL Spatial Data Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) and [MySQL Spatial Functions](https://dev.mysql.com/doc/refman/5.7/en/spatial-function-reference.html).
9
+
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).
10
10
11
11
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.
12
12
@@ -52,11 +52,14 @@ From the command line:
52
52
php artisan make:migration create_places_table
53
53
```
54
54
55
-
Then edit the migration you just created by adding at least one spatial data field:
55
+
Then edit the migration you just created by adding at least one spatial data field. For Laravel versions prior to 5.5, you can use the Blueprint provided by this package (Grimzy\LaravelMysqlSpatial\Schema\Blueprint):
56
56
57
57
```php
58
58
use Illuminate\Database\Migrations\Migration;
59
-
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
59
+
use Illuminate\Database\Schema\Blueprint;
60
+
61
+
// For Laravel < 5.5
62
+
// use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
60
63
61
64
class CreatePlacesTable extends Migration {
62
65
@@ -114,7 +117,8 @@ use Illuminate\Database\Eloquent\Model;
114
117
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:
174
-
175
-
-
176
-
`$table->geometry('column_name');`
177
-
178
-
-`$table->point('column_name');`
179
-
-`$table->lineString('column_name');`
180
-
-`$table->polygon('column_name');`
181
-
-`$table->multiPoint('column_name');`
182
-
-`$table->multiLineString('column_name');`
183
-
-`$table->multiPolygon('column_name');`
184
-
-`$table->geometryCollection('column_name');`
185
-
186
-
### Spatial indexes
187
-
188
-
You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.
189
-
190
-
-`$table->spatialIndex('column_name');`
191
-
-`$table->dropSpatialIndex(['column_name']);` or `$table->dropSpatialIndex('index_name')`
192
-
193
-
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
194
-
195
-
> For [`MyISAM`](https://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html) and (as of MySQL 5.7.5) `InnoDB` tables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using the `SPATIAL` keyword. Columns in spatial indexes must be declared `NOT NULL`.
196
-
197
-
Also please read this [**important note**](https://laravel.com/docs/5.5/migrations#indexes) regarding Index Lengths in the Laravel 5.6 documentation.
198
-
199
-
For example, as a follow up to the [Quickstart](#user-content-create-a-migration); from the command line, generate a new migration:
200
-
201
-
```shell
202
-
php artisan make:migration update_places_table
203
-
```
204
-
205
-
Then edit the migration file that you just created:
|`Polygon(LineString[])`*([exterior and interior boundaries](https://dev.mysql.com/doc/refman/5.7/en/gis-class-polygon.html))*|[Polygon](https://dev.mysql.com/doc/refman/5.7/en/gis-class-polygon.html)|
|`Polygon(LineString[])`*([exterior and interior boundaries](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html))*|[Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)|
The Geometry classes implement [`JsonSerializable`](http://php.net/manual/en/class.jsonserializable.php) and `Illuminate\Contracts\Support\Jsonable` to help serialize into GeoJSON:
310
234
311
235
```php
312
-
$point = new Point(10, 20);
236
+
$point = new Point(40.7484404, -73.9878441);
313
237
314
238
json_encode($point); // or $point->toJson();
315
239
@@ -329,7 +253,7 @@ json_encode($point); // or $point->toJson();
329
253
To deserialize a GeoJSON string into a Geometry class, you can use `Geometry::fromJson($json_string)` :
*Note that behavior and availability of MySQL spatial analysis functions differs in each MySQL version (cf. [documentation](https://dev.mysql.com/doc/refman/5.7/en/spatial-function-reference.html)).*
359
286
287
+
## Migrations
288
+
289
+
For Laravel versions prior to 5.5, you can use the Blueprint provided with this package: `Grimzy\LaravelMysqlSpatial\Schema\Blueprint`.
290
+
291
+
```php
292
+
use Illuminate\Database\Migrations\Migration;
293
+
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
294
+
295
+
class CreatePlacesTable extends Migration {
296
+
// ...
297
+
}
298
+
```
299
+
300
+
### Columns
301
+
302
+
Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints:
303
+
304
+
-`$table->geometry('column_name')`
305
+
-`$table->point('column_name')`
306
+
-`$table->lineString('column_name')`
307
+
-`$table->polygon('column_name')`
308
+
-`$table->multiPoint('column_name')`
309
+
-`$table->multiLineString('column_name')`
310
+
-`$table->multiPolygon('column_name')`
311
+
-`$table->geometryCollection('column_name')`
312
+
313
+
### Spatial indexes
314
+
315
+
You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints.
316
+
317
+
-`$table->spatialIndex('column_name')`
318
+
-`$table->dropSpatialIndex(['column_name'])` or `$table->dropSpatialIndex('index_name')`
319
+
320
+
Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html):
321
+
322
+
> For [`MyISAM`](https://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html) and (as of MySQL 5.7.5) `InnoDB` tables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using the `SPATIAL` keyword. Columns in spatial indexes must be declared `NOT NULL`.
323
+
324
+
Also please read this [**important note**](https://laravel.com/docs/5.5/migrations#indexes) regarding Index Lengths in the Laravel 5.6 documentation.
325
+
326
+
For example, as a follow up to the [Quickstart](#user-content-create-a-migration); from the command line, generate a new migration:
327
+
328
+
```shell
329
+
php artisan make:migration update_places_table
330
+
```
331
+
332
+
Then edit the migration file that you just created:
Schema::table('places', function (Blueprint $table) {
374
+
$table->point('location')->nullable()->change();
375
+
});
376
+
}
377
+
}
378
+
```
379
+
380
+
## Tests
381
+
382
+
```shell
383
+
composer test
384
+
# or
385
+
composer test:unit
386
+
composer test:integration
387
+
```
388
+
389
+
Integration tests require a running MySQL database. If you have Docker installed, you can start easily start one:
390
+
391
+
```shell
392
+
make start_db # starts MySQL 8.0
393
+
# or
394
+
make start_db V=5.7 # starts a MySQL 5.7
395
+
```
396
+
397
+
## Contributing
398
+
399
+
Recommendations and pull request are most welcome! Pull requests with tests are the best! There are still a lot of MySQL spatial functions to implement or creative ways to use spatial functions.
400
+
360
401
## Credits
361
402
362
403
Originally inspired from [njbarrett's Laravel postgis package](https://github.com/njbarrett/laravel-postgis).
0 commit comments