Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 3168ec6

Browse files
committed
Merge pull request #4 from MetalMatze/master
Add installation guide and improve migration and model example in README.md
2 parents a2c316d + 2897484 commit 3168ec6

File tree

1 file changed

+80
-43
lines changed

1 file changed

+80
-43
lines changed

README.md

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,71 @@ Laravel postgis extension
1414

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

17+
## Installation
18+
19+
composer require phaza/laravel-postgis
20+
21+
Next add the DatabaseServiceProvider to your `config/app.php` file.
22+
23+
'Phaza\LaravelPostgis\DatabaseServiceProvider',
24+
25+
That's all.
26+
1727
## Usage
1828

19-
First of all, enable postgis (See note further down)
29+
First of all, make sure to enable postgis.
30+
31+
CREATE EXTENSION postgis;
32+
33+
To verify that postgis is enabled
34+
35+
SELECT postgis_full_version();
2036

2137
### Migrations
2238

39+
Now create a model with a migration by running
40+
41+
php artisan make:model Location
42+
43+
If you don't want a model and just a migration run
44+
45+
php artisan make:migration create_locations_table
46+
47+
Open the created migrations with your editor.
48+
2349
```PHP
2450
use Illuminate\Database\Migrations\Migration;
2551
use Phaza\LaravelPostgis\Schema\Blueprint;
2652

27-
class CreateTestsTable extends Migration {
28-
29-
/**
30-
* Run the migrations.
31-
*
32-
* @return void
33-
*/
34-
public function up()
35-
{
36-
Schema::create('tests', function(Blueprint $table)
37-
{
38-
$table->increments('id');
39-
$table->polygon('myPolygon');
40-
$table->point('myPoint');
41-
$table->timestamps();
42-
});
43-
}
44-
45-
/**
46-
* Reverse the migrations.
47-
*
48-
* @return void
49-
*/
50-
public function down()
51-
{
52-
Schema::drop('tests');
53-
}
53+
class CreateLocationsTable extends Migration {
54+
55+
/**
56+
* Run the migrations.
57+
*
58+
* @return void
59+
*/
60+
public function up()
61+
{
62+
Schema::create('locations', function(Blueprint $table)
63+
{
64+
$table->increments('id');
65+
$table->string('name');
66+
$table->string('address')->unique();
67+
$table->point('location');
68+
$table->polygon('polygon');
69+
$table->timestamps();
70+
});
71+
}
72+
73+
/**
74+
* Reverse the migrations.
75+
*
76+
* @return void
77+
*/
78+
public function down()
79+
{
80+
Schema::drop('locations');
81+
}
5482

5583
}
5684
```
@@ -78,20 +106,34 @@ You must also define an associative array called `$postgisFields` which defines
78106
what attributes/columns on your model are to be considered geometry objects.
79107

80108
```PHP
81-
class TestModel extends Model {
82-
use PostgisTrait;
109+
use Illuminate\Database\Eloquent\Model;
110+
use Phaza\LaravelPostgis\Eloquent\PostgisTrait;
111+
use Phaza\LaravelPostgis\Geometries\Point;
112+
113+
class Location extends Model
114+
{
115+
use PostgisTrait;
116+
117+
protected $fillable = [
118+
'name',
119+
'address'
120+
];
121+
122+
protected $postgisFields = [
123+
'location' => Point::class,
124+
'polygon' => Polygon::class,
125+
];
83126

84-
protected $postgisFields = [
85-
'point' => Point::class
86-
];
87127
}
88128

89-
$testModel = new TestModel();
90-
$testModel->point = new Point(1,2);
91-
$testModel->save();
129+
$location1 = new Location();
130+
$location1->name = 'Googleplex';
131+
$location1->address = '1600 Amphitheatre Pkwy Mountain View, CA 94043';
132+
$location1->location = new Point(37.422009, -122.084047);
133+
$location1->save();
92134

93-
$testModel2 = TestModel::first();
94-
$testModel2->point instanceof Point // true
135+
$location2 = Location::first();
136+
$location2->location instanceof Point // true
95137
```
96138

97139
Available geometry classes:
@@ -103,8 +145,3 @@ Available geometry classes:
103145
* Polygon
104146
* MultiPolygon
105147
* GeometryCollection
106-
107-
## Enabling postgis
108-
109-
A method called enablePostgis() (and disablePostgis()) is included in the Blueprint object.
110-
They work on newer postgres installations, but I recommend enabling postgis manually for now.

0 commit comments

Comments
 (0)