From db9f3508acbd419af6e68f464d2d718b91535cd0 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Tue, 30 May 2017 20:07:40 +0100 Subject: [PATCH] move near method into a scope --- app/Http/Controllers/MicropubController.php | 3 ++- app/Place.php | 2 +- tests/Unit/PlacesTest.php | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index eb6f6ac1..b4b32459 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -6,6 +6,7 @@ use Ramsey\Uuid\Uuid; use App\{Media, Note, Place}; use Illuminate\Http\{Request, Response}; use App\Exceptions\InvalidTokenException; +use Phaza\LaravelPostgis\Geometries\Point; use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; use App\Services\{NoteService, PlaceService, TokenService}; @@ -313,7 +314,7 @@ class MicropubController extends Controller $matches ); $distance = (count($matches[0]) == 3) ? 100 * $matches[0][2] : 1000; - $places = Place::near($matches[0][0], $matches[0][1], $distance); + $places = Place::near(new Point($matches[0][0], $matches[0][1]))->get(); foreach ($places as $place) { $place->uri = config('app.url') . '/places/' . $place->slug; } diff --git a/app/Place.php b/app/Place.php index 75634337..466a345f 100644 --- a/app/Place.php +++ b/app/Place.php @@ -54,7 +54,7 @@ class Place extends Model * @param int Distance * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeNear(Builder $query, Point $point, $distance = 100) + public function scopeNear(Builder $query, Point $point, $distance = 1000) { $field = DB::raw( sprintf("ST_Distance(%s.location, ST_GeogFromText('%s'))", diff --git a/tests/Unit/PlacesTest.php b/tests/Unit/PlacesTest.php index 0a981725..d42189bf 100644 --- a/tests/Unit/PlacesTest.php +++ b/tests/Unit/PlacesTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit; use App\Place; use Tests\TestCase; +use Phaza\LaravelPostgis\Geometries\Point; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -16,7 +17,7 @@ class PlacesTest extends TestCase */ public function test_near_method() { - $nearby = Place::near(53.5, -2.38, 1000); + $nearby = Place::near(new Point(53.5, -2.38), 1000)->get(); $this->assertEquals('the-bridgewater-pub', $nearby[0]->slug); } }