Skip to content

Commit

Permalink
allow scoping a query within given bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
njbarrett committed Jun 15, 2016
1 parent accec37 commit 3c9ae38
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Eloquent/PostgisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ public function getPostgisFields()
}

}

/**
* Scope a query to only include items within the given bounds.
*
* @param $query \Illuminate\Database\Eloquent\Builder the current query object.
* @param $field string the field name to check.
* @param $bounds Polygon the polygon to check for items within.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWithin($query, $field, $bounds)
{
return $query->whereRaw("ST_Within($field::geometry, ST_GeomFromText('{$bounds->toWKT()}', 4326))");
}
}

0 comments on commit 3c9ae38

Please sign in to comment.