@@ -60,7 +60,7 @@ public function setRawAttributes(array $attributes, $sync = false)
6060 }
6161 }
6262
63- parent ::setRawAttributes ($ attributes , $ sync );
63+ return parent ::setRawAttributes ($ attributes , $ sync );
6464 }
6565
6666 public function getSpatialFields ()
@@ -73,7 +73,15 @@ public function getSpatialFields()
7373 }
7474
7575 public function scopeDistance ($ query , $ distance , $ geometry , $ column_name , $ exclude_self = false ) {
76- // TODO: what about mysql 5.5?
76+ $ query ->whereRaw ("st_distance(` {$ column_name }`, GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
77+
78+ if ($ exclude_self ) {
79+ $ query ->whereRaw ("st_distance(` {$ column_name }`, GeomFromText(' {$ geometry ->toWkt ()}')) != 0 " );
80+ }
81+ return $ query ;
82+ }
83+
84+ public function scopeDistanceSphere ($ query , $ distance , $ geometry , $ column_name , $ exclude_self = false ) {
7785 $ query ->whereRaw ("st_distance_sphere(` {$ column_name }`, GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
7886
7987 if ($ exclude_self ) {
@@ -82,7 +90,25 @@ public function scopeDistance($query, $distance, $geometry, $column_name, $exclu
8290 return $ query ;
8391 }
8492
93+ public function scopeDistanceValue ($ query , $ geometry , $ column_name ) {
94+ $ columns = $ query ->getQuery ()->columns ;
95+
96+ if (!$ columns ) {
97+ $ query ->select ('* ' );
98+ }
99+ $ query ->selectRaw ("st_distance(` {$ column_name }`, GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
100+ }
101+
102+ public function scopeDistanceSphereValue ($ query , $ geometry , $ column_name ) {
103+ $ columns = $ query ->getQuery ()->columns ;
104+
105+ if (!$ columns ) {
106+ $ query ->select ('* ' );
107+ }
108+ $ query ->selectRaw ("st_distance_sphere(` {$ column_name }`, GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
109+ }
110+
85111 public function scopeBounding ($ query , Geometry $ bounds , $ column_name ) {
86- return $ query ->whereRaw ("MBRIntersects (GeomFromText(' {$ bounds ->toWkt ()}'), ` {$ column_name }`) " );
112+ return $ query ->whereRaw ("st_intersects (GeomFromText(' {$ bounds ->toWkt ()}'), ` {$ column_name }`) " );
87113 }
88114}
0 commit comments