Skip to content

Commit

Permalink
Updates readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hutchison committed Feb 29, 2020
1 parent 6544516 commit 7b7662b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* [1.3.0](#400---2020-01-04) - Improves tests coverage and adds SQL Server driver
* [1.4.0](#400---2020-01-14) - Adds support for DB builder
* [1.4.1](#400---2020-02-10) - Fixes config publish command
* [1.5.0](#400---2020-02-29) - Adds orderByDistanceFrom methods
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,18 @@ $jobs2 = Job::withinDistanceOf(53.957962, -1.085485, 20, 'location1')
Any missing config options will be replaced with the defaults defined in `config('geoscope.defaults')`.
**Passing invalid config keys will also cause GeoScope to fallback to these defaults for all config fields.**

GeoScope also includes an `orderByDistanceFrom()` method that allows you to sort results by their distance from a specified lat long.

```php
// order by distance in ascending order
$results = Job::orderByDistanceFrom(30.1234, -71.2176, 'asc')->get();

// order by distance in descending order
$results = Job::orderByDistanceFrom(30.1234, -71.2176, 'desc')->get();
```

## Database Query Builder
Geoscope also allows you to call the `withinDistanceOf()` and `orWithinDistanceOf()` directly off the DB query builder:
Geoscope also allows you to call the `withinDistanceOf()`, `orWithinDistanceOf()` and `orderByDistanceFrom()` methods directly off the DB query builder:

```php
$results = DB::table('users')
Expand All @@ -133,6 +143,12 @@ and `orWithinDistanceOf()` methods:
'units' => 'meters'
])->get();
```
order by distance example:

```php
$results = DB::table('users')->orderByDistanceFrom(30.1234, -71.2176, 'asc')->get();
```


### Scope Drivers
Under the hood, GeoScope uses different drivers to ensure that the distance queries are optimised to the database connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ public function builder_macro_returns_correct_results_for_or_within_distance_of(
$this->assertEquals($expected[19]->id, $actual[19]->id);
$this->assertEquals($expected[34]->id, $actual[34]->id);
}

/**
* @test
*/
public function builder_macro_order_by_distance_from_returns_correct_results()
{
$centralPoint = $this->getLatLongs()->get('central_point');

factory(Test::class, 30)->create();

$results1 = DB::table('tests')->orderByDistanceFrom($centralPoint['latitude'], $centralPoint['longitude'], 'asc')->get();
$results2 = DB::table('tests')->orderByDistanceFrom($centralPoint['latitude'], $centralPoint['longitude'], 'desc')->get();

$this->assertEquals($results1->pluck('id'), $results2->reverse()->pluck('id'));
}
}

0 comments on commit 7b7662b

Please sign in to comment.