PHP code example of bertim / laravel-mysql-spatial
1. Go to this page and download the library: Download bertim/laravel-mysql-spatial library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
// For Laravel < 5.5
// use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
class CreatePlacesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('places', function(Blueprint $table)
{
$table->increments('id');
$table->string('name')->unique();
// Add a Point spatial data field named location
$table->point('location')->nullable();
// Add a Polygon spatial data field named area
$table->polygon('area')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('places');
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
/**
* @property \Grimzy\LaravelMysqlSpatial\Types\Point $location
* @property \Grimzy\LaravelMysqlSpatial\Types\Polygon $area
*/
class Place extends Model
{
use SpatialTrait;
protected $fillable = [
'name'
];
protected $spatialFields = [
'location',
'area'
];
}
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
$place1 = new Place();
$place1->name = 'Empire State Building';
// saving a point
$place1->location = new Point(40.7484404, -73.9878441); // (lat, lng)
$place1->save();
// saving a polygon
$place1->area = new Polygon([new LineString([
new Point(40.74894149554006, -73.98615270853043),
new Point(40.74848633046773, -73.98648262023926),
new Point(40.747925497790725, -73.9851602911949),
new Point(40.74837050671544, -73.98482501506805),
new Point(40.74894149554006, -73.98615270853043)
])]);
$place1->save();
$place1->area = new Polygon();
use Illuminate\Database\Migrations\Migration;
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
class CreatePlacesTable extends Migration {
// ...
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdatePlacesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// MySQL < 5.7.5: table has to be MyISAM
// \DB::statement('ALTER TABLE places ENGINE = MyISAM');
Schema::table('places', function (Blueprint $table) {
// Make sure point is not nullable
$table->point('location')->change();
// Add a spatial index on the location field
$table->spatialIndex('location');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('places', function (Blueprint $table) {
$table->dropSpatialIndex(['location']); // either an array of column names or the index name
});
// \DB::statement('ALTER TABLE places ENGINE = InnoDB');
Schema::table('places', function (Blueprint $table) {
$table->point('location')->nullable()->change();
});
}
}
shell
php artisan migrate
shell
php artisan make:model Place
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.