Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@julienbourdeau
Last active July 13, 2017 19:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienbourdeau/780f28b6b09c42a663dbd3fa65d427f3 to your computer and use it in GitHub Desktop.
Save julienbourdeau/780f28b6b09c42a663dbd3fa65d427f3 to your computer and use it in GitHub Desktop.
Add geolocalization capability to Laravel Scout (with Algolia engine)
<?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);
});
<?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