|
1 | 1 | # Laravel MySQL Spatial extension |
2 | 2 |
|
3 | | -Heavily inspired from the [Laravel postgis extension](https://github.com/njbarrett/laravel-postgis) package. |
| 3 | +[](https://travis-ci.org/grimzy/laravel-mysql-spatial) |
| 4 | +[](https://codeclimate.com/github/grimzy/laravel-mysql-spatial) |
| 5 | +[](https://codeclimate.com/github/grimzy/laravel-mysql-spatial/coverage) |
| 6 | +[](https://packagist.org/packages/grimzy/laravel-mysql-spatial) |
| 7 | +[](https://packagist.org/packages/grimzy/laravel-mysql-spatial) |
| 8 | +[](LICENSE) |
| 9 | + |
| 10 | +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). |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Add the package using composer: |
| 17 | + |
| 18 | +```shell |
| 19 | +composer require grimzy/laravel-mysql-spatial |
| 20 | +``` |
| 21 | + |
| 22 | +Register the service provider in `config/app.php`: |
| 23 | + |
| 24 | +```php |
| 25 | +'providers' => [ |
| 26 | + /* |
| 27 | + * Package Service Providers... |
| 28 | + */ |
| 29 | + Grimzy\LaravelMysqlSpatial\SpatialServiceProvider::class, |
| 30 | +], |
| 31 | +``` |
| 32 | + |
| 33 | +## Quickstart |
| 34 | + |
| 35 | +### Create a migration |
| 36 | + |
| 37 | +From the command line: |
| 38 | + |
| 39 | +```shell |
| 40 | +php artisan make:migration create_places_table |
| 41 | +``` |
| 42 | + |
| 43 | +Then edit the migration you just created by adding at least one spatial data field: |
| 44 | + |
| 45 | +```php |
| 46 | +use Illuminate\Database\Migrations\Migration; |
| 47 | +use Grimzy\LaravelMysqlSpatial\Schema\Blueprint; |
| 48 | + |
| 49 | +class CreatePlacesTable extends Migration { |
| 50 | + |
| 51 | + /** |
| 52 | + * Run the migrations. |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function up() |
| 57 | + { |
| 58 | + Schema::create('places', function(Blueprint $table) |
| 59 | + { |
| 60 | + $table->increments('id'); |
| 61 | + $table->string('name')->unique(); |
| 62 | + // Add a Point spatial data field named location |
| 63 | + $table->point('location')->nullable(); |
| 64 | + $table->timestamps(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Reverse the migrations. |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function down() |
| 74 | + { |
| 75 | + Schema::drop('places'); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Run the migration: |
| 81 | + |
| 82 | +```shell |
| 83 | +php artisan migrate |
| 84 | +``` |
| 85 | + |
| 86 | +### Create a model |
| 87 | + |
| 88 | +From the command line: |
| 89 | + |
| 90 | +```shell |
| 91 | +php artisan make:model Place |
| 92 | +``` |
| 93 | + |
| 94 | +Then edit the model you just created. It must use the `SpatialTrait` and define an array called `$spatialFields` with the name of the MySQL Spatial Data field(s) created in the migration: |
| 95 | + |
| 96 | +```php |
| 97 | +namespace App; |
| 98 | + |
| 99 | +use Illuminate\Database\Eloquent\Model; |
| 100 | +use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; |
| 101 | + |
| 102 | +/** |
| 103 | + * @property \Grimzy\LaravelMysqlSpatial\Types\Point $location |
| 104 | + */ |
| 105 | +class Place extends Model |
| 106 | +{ |
| 107 | + use SpatialTrait; |
| 108 | + |
| 109 | + protected $fillable = [ |
| 110 | + 'name', |
| 111 | + ]; |
| 112 | + |
| 113 | + protected $spatialFields = [ |
| 114 | + 'location', |
| 115 | + ]; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Saving a model |
| 120 | + |
| 121 | +```php |
| 122 | +$place1 = new Place(); |
| 123 | +$place1->name = 'Empire State Building'; |
| 124 | +$place1->location = new Point(40.7484404, -73.9878441); |
| 125 | +$place1->save(); |
| 126 | +``` |
| 127 | + |
| 128 | +### Retrieving a model |
| 129 | + |
| 130 | +```php |
| 131 | +$place2 = Place::first(); |
| 132 | +$lat = $place2->location->getLat(); // 40.7484404 |
| 133 | +$lng = $place2->location->getLng(); // -73.9878441 |
| 134 | +``` |
| 135 | + |
| 136 | +## Migration |
| 137 | + |
| 138 | +Available [MySQL Spatial Types](https://dev.mysql.com/doc/refman/5.7/en/spatial-datatypes.html) migration blueprints: |
| 139 | + |
| 140 | +- geometry |
| 141 | +- point |
| 142 | +- lineString |
| 143 | +- polygon |
| 144 | +- multiPoint |
| 145 | +- multiLineString |
| 146 | +- multiPolygon |
| 147 | +- geometryCollection |
| 148 | + |
| 149 | +### Spatial index |
| 150 | + |
| 151 | +You can add or drop spatial indexes in your migrations with the `spatialIndex` and `dropSpatialIndex` blueprints. |
| 152 | + |
| 153 | +Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html): |
| 154 | + |
| 155 | +> 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`. |
| 156 | +
|
| 157 | +## Models |
| 158 | + |
| 159 | +Available geometry classes: |
| 160 | + |
| 161 | +- Point |
| 162 | +- LineString |
| 163 | +- Polygon |
| 164 | +- MultiPoint |
| 165 | +- MultiLineString |
| 166 | +- MultiPolygon |
| 167 | +- GeometryCollection |
| 168 | + |
| 169 | + |
| 170 | +## Credits |
| 171 | + |
| 172 | +Originally inspired from [njbarrett's Laravel postgis package](https://github.com/njbarrett/laravel-postgis). |
0 commit comments