mammutgroup/laravel-spatial
Laravel Mysql Geometry extension
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!))
Versions
Use 2.* for Laravel 5.1.*
Use 3.* for Laravel 5.2.*
Installation
composer require phaza/laravel-postgis
Next add the DatabaseServiceProvider to your config/app.php file.
'Mammutgroup\LaravelPostgis\DatabaseServiceProvider',
That's all.
Usage
First of all, make sure to enable postgis.
CREATE EXTENSION postgis;
To verify that postgis is enabled
SELECT postgis_full_version();
Migrations
Now create a model with a migration by running
php artisan make:model Location
If you don't want a model and just a migration run
php artisan make:migration create_locations_table
Open the created migrations with your editor.
use Illuminate\Database\Migrations\Migration;
use Mammutgroup\LaravelPostgis\Schema\Blueprint;
class CreateLocationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('locations', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('address')->unique();
$table->point('location');
$table->polygon('polygon');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('locations');
}
}
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 array called $postgisFields which defines
what attributes/columns on your model are to be considered geometry objects.
use Illuminate\Database\Eloquent\Model;
use Mammutgroup\LaravelPostgis\Eloquent\PostgisTrait;
use Mammutgroup\LaravelPostgis\Geometries\Point;
class Location extends Model
{
use PostgisTrait;
protected $fillable = [
'name',
'address'
];
protected $postgisFields = [
'location',
'polygon',
];
}
$location1 = new Location();
$location1->name = 'Googleplex';
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
$location1->location = new Point(37.422009, -122.084047);
$location1->save();
$location2 = Location::first();
$location2->location instanceof Point // true
Available geometry classes:
- Point
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- GeometryCollection
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 3.1.3 | >=5.5 | ^5.2 | MIT | |
| 3.1.2 | >=5.5 | ^5.2 | MIT | |
| 3.1.1 | >=5.5 | ^5.2 | MIT | |
| 3.1 | >=5.5 | ^5.2 | MIT | |
| 3.0 | >=5.5 | ^5.2 | MIT | |
| 2.18 | >=5.5 | ~5.1.0 | MIT | |
| 2.17 | >=5.5 | ~5.1.0 | MIT | |
| 2.16 | >=5.5 | ~5.1 | MIT | |
| 2.15 | >=5.5 | ~5.1 | MIT | |
| 2.14 | >=5.5 | ~5.0 | MIT | |
| 2.13 | >=5.5 | ~5.0 | MIT | |
| 2.12 | >=5.5 | ~5.0 | MIT | |
| 2.11 | >=5.5 | ~5.0 | MIT | |
| 2.10 | >=5.5 | ~5.0 | MIT | |
| 2.9 | >=5.5 | ~5.0 | MIT | |
| 2.8 | >=5.5 | ~5.0 | MIT | |
| 2.7 | >=5.5 | ~5.0 | MIT | |
| 2.6 | >=5.5 | ~5.0 | MIT | |
| 2.5 | >=5.5 | ~5.0 | MIT | |
| 2.4 | >=5.5 | ~5.0 | MIT | |
| 2.3 | >=5.5 | ~5.0 | MIT | |
| 2.2 | >=5.5 | ~5.0 | MIT | |
| 2.1 | >=5.5 | ~5.0 | MIT | |
| 2.0 | >=5.5 | ~5.0 | MIT | |
| 1.2 | >=5.5 | ~5.0 | MIT | |
| 1.1 | >=5.5 | ~5.0 | MIT | |
| 1.0 | >=5.5 | ~5.0 | MIT | |
| 0.2 | >=5.5 | ~5.0 | MIT | |
| 0.1 | >=5.5 | ~5.0 | MIT |