Skip to content

Commit 7b7662b

Browse files
author
Adam Hutchison
committed
Updates readme
1 parent 6544516 commit 7b7662b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1919
* [1.3.0](#400---2020-01-04) - Improves tests coverage and adds SQL Server driver
2020
* [1.4.0](#400---2020-01-14) - Adds support for DB builder
2121
* [1.4.1](#400---2020-02-10) - Fixes config publish command
22+
* [1.5.0](#400---2020-02-29) - Adds orderByDistanceFrom methods

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,18 @@ $jobs2 = Job::withinDistanceOf(53.957962, -1.085485, 20, 'location1')
113113
Any missing config options will be replaced with the defaults defined in `config('geoscope.defaults')`.
114114
**Passing invalid config keys will also cause GeoScope to fallback to these defaults for all config fields.**
115115

116+
GeoScope also includes an `orderByDistanceFrom()` method that allows you to sort results by their distance from a specified lat long.
117+
118+
```php
119+
// order by distance in ascending order
120+
$results = Job::orderByDistanceFrom(30.1234, -71.2176, 'asc')->get();
121+
122+
// order by distance in descending order
123+
$results = Job::orderByDistanceFrom(30.1234, -71.2176, 'desc')->get();
124+
```
125+
116126
## Database Query Builder
117-
Geoscope also allows you to call the `withinDistanceOf()` and `orWithinDistanceOf()` directly off the DB query builder:
127+
Geoscope also allows you to call the `withinDistanceOf()`, `orWithinDistanceOf()` and `orderByDistanceFrom()` methods directly off the DB query builder:
118128

119129
```php
120130
$results = DB::table('users')
@@ -133,6 +143,12 @@ and `orWithinDistanceOf()` methods:
133143
'units' => 'meters'
134144
])->get();
135145
```
146+
order by distance example:
147+
148+
```php
149+
$results = DB::table('users')->orderByDistanceFrom(30.1234, -71.2176, 'asc')->get();
150+
```
151+
136152

137153
### Scope Drivers
138154
Under the hood, GeoScope uses different drivers to ensure that the distance queries are optimised to the database connection

tests/Integration/ScopeDrivers/Traits/ScopeDriverDBBuilderTests.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,19 @@ public function builder_macro_returns_correct_results_for_or_within_distance_of(
5050
$this->assertEquals($expected[19]->id, $actual[19]->id);
5151
$this->assertEquals($expected[34]->id, $actual[34]->id);
5252
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function builder_macro_order_by_distance_from_returns_correct_results()
58+
{
59+
$centralPoint = $this->getLatLongs()->get('central_point');
60+
61+
factory(Test::class, 30)->create();
62+
63+
$results1 = DB::table('tests')->orderByDistanceFrom($centralPoint['latitude'], $centralPoint['longitude'], 'asc')->get();
64+
$results2 = DB::table('tests')->orderByDistanceFrom($centralPoint['latitude'], $centralPoint['longitude'], 'desc')->get();
65+
66+
$this->assertEquals($results1->pluck('id'), $results2->reverse()->pluck('id'));
67+
}
5368
}

0 commit comments

Comments
 (0)