| Conditions | 1 |
| Paths | 1 |
| Total Lines | 19 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 2 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 23 | public function scopeNearestLocation(Builder $query, $latitude, $longitude) |
||
| 24 | { |
||
| 25 | $fieldLatitude = 'address_latitude'; |
||
| 26 | $fieldLongitude = 'address_longitude'; |
||
| 27 | |||
| 28 | $selectQuery = sprintf( |
||
| 29 | '*, ' . |
||
| 30 | '6371 * acos(cos(radians(?)) ' . |
||
| 31 | '* cos(radians(%s)) ' . |
||
| 32 | '* cos(radians(%s) - radians(?)) ' . |
||
| 33 | '+ sin(radians(?)) ' . |
||
| 34 | '* sin(radians(%s))) AS distance', |
||
| 35 | $fieldLatitude, $fieldLongitude, $fieldLatitude |
||
| 36 | ); |
||
| 37 | |||
| 38 | return $query->selectRaw( |
||
|
|
|||
| 39 | $selectQuery, |
||
| 40 | [$latitude, $longitude, $latitude] |
||
| 41 | )->orderBy('distance'); |
||
| 42 | } |
||
| 44 |