Last active
July 13, 2017 19:53
-
-
Save julienbourdeau/780f28b6b09c42a663dbd3fa65d427f3 to your computer and use it in GitHub Desktop.
Add geolocalization capability to Laravel Scout (with Algolia engine)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Simple example | |
// We look for everything with 100 Kms of CDG (empty query) | |
Airport::searchAround('', 49.012779, 2.55, 100); | |
// Example with callback | |
// Just like Laravel\Scout\Searchable::search you can pass it a callback | |
Airport::searchAround('', 49.012779, 2.55, 100, function ($algolia, $query, $options) { | |
dump($options); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Search; | |
/** | |
* This trait must be used next to the Laravel\Scout\Searchable trait | |
* | |
*/ | |
trait GeoSearchable | |
{ | |
public static function searchAround($query, $lat, $lng, $radius = 10, $callback = null) | |
{ | |
$location = [ | |
'aroundLatLng' => $lat.','.$lng, | |
'aroundRadius' => $radius * 1000 | |
]; | |
return static::search($query, function ($algolia, $query, $options) use ($location, $callback) { | |
$options = array_merge($options, $location); | |
if ($callback) { | |
return call_user_func( | |
$callback, | |
$algolia, | |
$query, | |
$options | |
); | |
} | |
return $algolia->search($query, $options); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment