Algolia Issues with Laravel

I’m using algolia with laravel for my searching. That’s working fine, but when I added geolocation, I ran into a problem. I have fields in the db for lat and lng so added this into the model:

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

        $record['_geoloc'] = [
            'lat' => $record['latitude'],
            'lng' => $record['longitude'],
        ];

        unset($record['created_at'], $record['updated_at']); // Remove unrelevant data
        unset($record['latitude'], $record['longitude']);

        return $record;
    }

However, when I tried to save a model, I got this error:

_geoloc.lat or _geoloc.lng attributes cannot be null near line:1 > column:526

So I then I added a '_geoloc field to the db, which was a TEXT.
But now, I’m getting this error when trying to save my model:

AlgoliaSearch \ AlgoliaException (400)
_geoloc attribute cannot contains a string near line:1 column:606

From the error message:

$answer = Json::decode($response, true);

   if (intval($http_status / 100) == 4) {
       throw new AlgoliaException(isset($answer['message']) ?     > $answer['message'] : $http_status.' error', $http_status);

Here’s the saving of the model:

        //gets an array of lat and lng
        //logger($gps) yields an array
        //logger($geoJson) yields {"lat":34.8802845,"lng":-77.1041536}
        $gps = Map::getListingDetails($request);
        $geoJson = json_encode($gps);
        
        $listing = Listing::create([
                'user_id' => auth()->id(),
                '_geoloc' => $gps,               
         ]);

Am I doing something wrong from the beginning, or at the end? With the toSearchAbleArray, shouldn’t the algolia code be able to decode the json string saved in the db? Thanks in advance!

I’m not sure what is sent anymore. Can you share your final table structure and the toSearchableArray method ?

_geoloc.lat or _geoloc.lng attributes cannot be null near line:1 > column:526

I’m not sure why this happened, there shouldn’t be a any use of _geoloc in the model, only when it’s converted to an array before being sent to Algolia.