Algolia geo search how to structure db


I am trying to adapt to algolia geo search, from my testing without a field _geoloc, algolia geo search doesn’t work.

Now my database looke like this:

<img src=“//"width="690” height=“335”>

However when I try to run laravel scout I get:

_geoloc attribute cannot contains a string near line:1 column:165

I assume that is due to it’s format, but how can I forgot the databse so it has the right structure?

Hi there,

The _geoloc attribute needs to contains floating point numbers. I seems you are sending strings.
I’m not a Laravel expert so I don’t know how you can transform the data. Maybe try in the method getAlgoliaRecord: GitHub - algolia/algoliasearch-laravel: [Deprecated] We now recommend using Laravel Scout, see => ?

Hi, I did fix it now. For it to work I had to create a table called _geoloc and inside it have lag and lng float fields with the data. That table was then attached (joined) to a business table which actually works. It is a bit weird when I tried to input _geoloc as a field in a table it wouldn’t does it have to be in that format? Before I had locations table with lat and lng as floats but the geo search always returned null


If you want to keep lat and lng as separate columns, you will simply have to prepare your record before it’s sent to Algolia. Laravel Scout provide a method for that: Laravel Scout - Laravel - The PHP Framework For Web Artisans

In this case you will have

    public function toSearchableArray()
        $record = $this->toArray();

        $record['_geoloc'] = [
            'lat' => $record['lat'],
            'lng' => $record['lng'],

        unset($record['lat'], $record['lng']);

        return $record;

If you prefer having a _geoloc column you can use cast feature of Laravel. I think you got the “_geoloc attribute cannot contains a string” error because the column wasn’t json decoded.

Please let me know if that answers your question.

Just a quick question regarding your answer, so I have a businesses and _geoloc table. _geoloc is joined go businesses and that way the algolia geo search works fine. Does the algolia geo search actually works without having it named _geoloc? If I have lat and lng separate not inside _geoloc it seems I cannot for example search by radius, if it is inside _geoloc it works fine