Skip to content

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.

License

Notifications You must be signed in to change notification settings

mtdavidson/laravel-postgis

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel postgis extension

Build Status Code Climate Coverage Status

Features

  • Work with geometry classes instead of arrays. ($myModel->myPoint = new Point(1,2))
  • Adds helpers in migrations. ($table->polygon('myColumn'))

Future plans

  • Geometry functions on the geometry classes (contains(), equals(), distance(), etc… (HELP!))

Usage

First of all, enable postgis (See note further down)

Migrations

use Illuminate\Database\Migrations\Migration;
use Phaza\LaravelPostgis\Schema\Blueprint;

class CreateTestsTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('tests', function(Blueprint $table)
		{
			$table->increments('id');
			$table->polygon('myPolygon');
			$table->point('myPoint');
			$table->timestamps();
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('tests');
	}

}

Available blueprint geometries:

  • point
  • multipoint
  • linestring
  • multilinestring
  • polygon
  • multipolygon
  • geometrycollection

other methods:

  • enablePostgis
  • disablePostgis

Models

All models which are to be PostGis enabled must use the PostgisTrait.

You must also define an associative array called $postgisFields which defines what attributes/columns on your model are to be considered geometry objects.

class TestModel extends Model {
	use PostgisTrait;

	protected $postgisFields = [
		'point' => Point::class
	];
}

$testModel = new TestModel();
$testModel->point = new Point(1,2);
$testModel->save();

$testModel2 = TestModel::first();
$testModel2->point instanceof Point // true

Available geometry classes:

  • Point
  • MultiPoint
  • LineString
  • MultiLineString
  • Polygon
  • MultiPolygon
  • GeometryCollection

Enabling postgis

A method called enablePostgis() (and disablePostgis()) is included in the Blueprint object. They work on newer postgres installations, but I recommend enabling postgis manually for now.

About

Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%