@@ -11,7 +11,7 @@ final class SQLServerScopeDriver extends AbstractScopeDriver
1111 */
1212 public function withinDistanceOf (float $ lat , float $ long , float $ distance )
1313 {
14- return $ this ->query ->whereRaw ($ this ->getSQL (), [
14+ return $ this ->query ->whereRaw ($ this ->getWithinDistanceSQL (), [
1515 $ lat ,
1616 $ long ,
1717 $ distance ,
@@ -25,22 +25,50 @@ public function withinDistanceOf(float $lat, float $long, float $distance)
2525 */
2626 public function orWithinDistanceOf (float $ lat , float $ long , float $ distance )
2727 {
28- return $ this ->query ->orWhereRaw ($ this ->getSQL (), [
28+ return $ this ->query ->orWhereRaw ($ this ->getWithinDistanceSQL (), [
2929 $ lat ,
3030 $ long ,
3131 $ distance ,
3232 ]);
3333 }
3434
35+ /**
36+ * @throws InvalidOrderDirectionParameter
37+ * @param float $lat
38+ * @param float $long
39+ * @param float $orderDirection
40+ */
41+ public function orderByDistanceFrom (float $ lat , float $ long , string $ orderDirection = 'asc ' )
42+ {
43+ $ this ->checkOrderDirectionIdentifier ($ orderDirection );
44+
45+ return $ this ->query ->orderByRaw ($ this ->getOrderByDistanceSQL ($ orderDirection ), [
46+ $ long ,
47+ $ lat ,
48+ ]);
49+ }
50+
3551 /**
3652 * @return string
3753 */
38- private function getSQL (): string
54+ private function getWithinDistanceSQL (): string
3955 {
4056 return <<<EOD
4157 (GEOGRAPHY::Point(?, ?, 4326)
4258 .STDistance(GEOGRAPHY::Point( {$ this ->config ["lat-column " ]}, {$ this ->config ["long-column " ]}, 4326)))
4359 * {$ this ->conversion } < ?
60+ EOD ;
61+ }
62+
63+ /**
64+ * @return string
65+ */
66+ private function getOrderByDistanceSQL (string $ orderDirection ): string
67+ {
68+ return <<<EOD
69+ (GEOGRAPHY::Point(?, ?, 4326)
70+ .STDistance(GEOGRAPHY::Point( {$ this ->config ["lat-column " ]}, {$ this ->config ["long-column " ]}, 4326)))
71+ * {$ this ->conversion } {$ orderDirection }
4472EOD ;
4573 }
4674}
0 commit comments