PHP code example of colesnic89 / laravel-mysql-spatial
1. Go to this page and download the library: Download colesnic89/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/ */
colesnic89 / laravel-mysql-spatial example snippets
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();
});
// Or create the spatial fields with an SRID (e.g. 4326 WGS84 spheroid)
// Schema::create('places', function(Blueprint $table)
// {
// $table->increments('id');
// $table->string('name')->unique();
// // Add a Point spatial data field named location with SRID 4326
// $table->point('location', 4326)->nullable();
// // Add a Polygon spatial data field named area with SRID 4326
// $table->polygon('area', 4326)->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;
use Grimzy\LaravelMysqlSpatial\Types\LineString;
$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();
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
use Grimzy\LaravelMysqlSpatial\Types\LineString;
$place1 = new Place();
$place1->name = 'Empire State Building';
// saving a point with SRID 4326 (WGS84 spheroid)
$place1->location = new Point(40.7484404, -73.9878441, 4326); // (lat, lng, srid)
$place1->save();
// saving a polygon with SRID 4326 (WGS84 spheroid)
$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)
])], 4326);
$place1->save();
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.