@@ -14,43 +14,71 @@ Laravel postgis extension
14
14
15
15
* Geometry functions on the geometry classes (contains(), equals(), distance(), etc… (HELP!))
16
16
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
+
17
27
## Usage
18
28
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();
20
36
21
37
### Migrations
22
38
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
+
23
49
``` PHP
24
50
use Illuminate\Database\Migrations\Migration;
25
51
use Phaza\LaravelPostgis\Schema\Blueprint;
26
52
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
+ }
54
82
55
83
}
56
84
```
@@ -78,20 +106,34 @@ You must also define an associative array called `$postgisFields` which defines
78
106
what attributes/columns on your model are to be considered geometry objects.
79
107
80
108
``` 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
+ ];
83
126
84
- protected $postgisFields = [
85
- 'point' => Point::class
86
- ];
87
127
}
88
128
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();
92
134
93
- $testModel2 = TestModel ::first();
94
- $testModel2->point instanceof Point // true
135
+ $location2 = Location ::first();
136
+ $location2->location instanceof Point // true
95
137
```
96
138
97
139
Available geometry classes:
@@ -103,8 +145,3 @@ Available geometry classes:
103
145
* Polygon
104
146
* MultiPolygon
105
147
* 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