Skip to content

Commit 8bd4014

Browse files
committed
Added distance scope
1 parent a7a0f63 commit 8bd4014

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/Eloquent/SpatialTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function getSpatialFields()
6969
} else {
7070
throw new SpatialFieldsNotDefinedException(__CLASS__ . ' has to define $spatialFields');
7171
}
72+
}
7273

74+
public function scopeDistance($query, $distance, $point, $column_name) {
75+
// TODO: check if array, and transform to string delimited by ,
76+
// TODO: remove the objects with a distance of 0?
77+
return $query->whereRaw("st_distance_sphere(`{$column_name}`, POINT({$point->getLng()}, {$point->getLat()})) <= {$distance}");
7378
}
7479
}

0 commit comments

Comments
 (0)