PHP code example of angel-source-labs / laravel-spatial
1. Go to this page and download the library: Download angel-source-labs/laravel-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/ */
angel-source-labs / laravel-spatial example snippets
use Illuminate\Database\Migrations\Migration;
// use SpatialBlueprint for Spatial features and for proper code completion
use AngelSourceLabs\LaravelSpatial\Schema\SpatialBlueprint as 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_with_srid', 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();
});
// In Postgis, you can also create spatial fields that are Geography types instead of Geometry types
Schema::create('places_with_geography', 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, Blueprint::GEOGRAPHY)->nullable();
// Add a Polygon spatial data field named area with SRID 4326
$table->polygon('area', 4326, Blueprint::GEOGRAPHY)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('places');
}
}
namespace App;
use AngelSourceLabs\LaravelSpatial\Eloquent\SpatialTrait;
use Illuminate\Database\Eloquent\Model;
/**
* @property \AngelSourceLabs\LaravelSpatial\Types\Point $location
* @property \AngelSourceLabs\LaravelSpatial\Types\Polygon $area
*/
class Place extends Model
{
use SpatialTrait;
protected $fillable = [
'name'
];
protected $spatialFields = [
'location',
'area'
];
}
use AngelSourceLabs\LaravelSpatial\Types\LineString;
use AngelSourceLabs\LaravelSpatial\Types\Point;
use AngelSourceLabs\LaravelSpatial\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();
use AngelSourceLabs\LaravelSpatial\Types\LineString;
use AngelSourceLabs\LaravelSpatial\Types\Point;
use AngelSourceLabs\LaravelSpatial\Types\Polygon;
$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 AngelSourceLabs\LaravelSpatial\Schema\SpatialBlueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePlacesTable extends Migration {
// ...
}
use Illuminate\Database\Migrations\Migration;
use AngelSourceLabs\LaravelSpatial\Schema\SpatialBlueprint as 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.