-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HasGeometryScropes trait and a sample implementation of within
- Loading branch information
1 parent
16e77b8
commit a1e1cdf
Showing
2 changed files
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace Phaza\LaravelPostgis\Eloquent; | ||
|
||
trait HasGeometryScopes | ||
{ | ||
public function scopeWithin($query, $geometryColumn, $geometry) | ||
{ | ||
// SQL syntax: "ST_Within(geometry A, geometry B)" | ||
// Example: "ST_Within(point::geometry, ST_GeomFromText('POLYGON ((30 10, 20 40, 10 20, 30 10))', 4326 ))" | ||
|
||
// Preparing parts | ||
$geometryA = $geometryColumn . "::geometry"; | ||
$geometryB = "ST_GeomFromText('" . $geometry->toWKT() . "', " . $this->postgisTypes[$geometryColumn]['srid'] . ")"; | ||
|
||
// Assumption is made that geometry A and geometry B has the same SRID | ||
return $query->whereRaw("ST_Within(" . $geometryA . "," . $geometryB . ")"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters