Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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