move near method into a scope

This commit is contained in:
Jonny Barnes 2017-05-30 20:07:40 +01:00
parent 451437ed6a
commit db9f3508ac
3 changed files with 5 additions and 3 deletions

View file

@ -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;
}

View file

@ -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'))",

View file

@ -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);
}
}