Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlgoliaSearch \ AlgoliaException (400) _geoloc attribute cannot contains a string #554

Closed
mikestratton opened this issue May 31, 2019 · 9 comments

Comments

@mikestratton
Copy link

Cannot send location data (_geoloc{ 'lat'=> 0.00, 'lng'=>0.00}) to Algolia from Laravel application.

Here is the error:
AlgoliaSearch \ AlgoliaException (400) _geoloc attribute cannot contains a string near line:1 column:587 C:\xampp\htdocs\laravel\vendor\algolia\algoliasearch-client-php\src\AlgoliaSearch\Client.php

Here is the code in the controller:
$geoloc = array('lat'=>$lat,'lng'=>$lng); $input['_geoloc'] = json_encode($geoloc);

@mikestratton
Copy link
Author

Everything else is working with Algolia Search and Laravel. Algolia just will not accept latitude/longitude in the required _geoloc format.

@nunomaduro
Copy link
Contributor

@mikestratton Thanks for reporting this! We will take a look next Monday.

@mikestratton
Copy link
Author

mikestratton commented Jun 2, 2019

More information:

// composer.json
"algolia/algoliasearch-client-php": "^1.27",
"algolia/laravel-scout-algolia-macros": "^0.6.0",
"laravel/scout": "^5.0",

// In the model
use Laravel\Scout\Searchable;
use Searchable;

// Migration
$table->string('_geoloc')->nullable();

Database
table name: '_geoloc'
type varchar(191)
Table row created when form submitted
Column: _geoloc:
Value: {"lat":41.2461518,"lng":-81.3509416}

/* Latitude and Longitude are generated by submitting zip code to Google's GeoCoding API. Array is returned $geo[13.456, 91.2345] */
$zip = $request->zip_code;
$geo = $this->geoCoding($zip);
$lat = $geo[0];
$lng = $geo[1];
$input['lat'] = $lat;
$input['lng'] = $lng;

@TomKlotzPro
Copy link
Contributor

TomKlotzPro commented Jun 3, 2019

Hi @mikestratton, thanks for reporting this issue.

Keep in mind that _geoloc should be an array, so you can't use a json_encode on it.
To make it work, you should send the _geoloc as array:

$geoloc = array('lat'=>$lat,'lng'=>$lng);
$input['_geoloc'] = $geoloc;

Give it a try, and tell me if it works for you.

@mikestratton
Copy link
Author

I already tried that - I get an array to string conversion error from illuminate.

ErrorException (E_NOTICE) Array to string conversion C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Support\Str.php
I then convert to a string with implode and get the original error.

@mikestratton
Copy link
Author

mikestratton commented Jun 3, 2019

Can't save an array in the database without serialize or conversion to string or object. Algolia is receiving the data after it has been stored as a record in MySQL, correct?

@mikestratton
Copy link
Author

mikestratton commented Jun 3, 2019

Serialize showing the same AlgoliaSearch string error, obviously.

$geoloc = array('lat'=>$lat,'lng'=>$lng);          
$input['_geoloc'] = serialize($geoloc);

@TomKlotzPro
Copy link
Contributor

TomKlotzPro commented Jun 4, 2019

Thanks @mikestratton for providing me more details.

I've tested your problem locally and I have the same issue. This is what you should do to avoid the conflicts between your local storage and your Algolia index.

On your model you must add this :

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

    $input['_geoloc'] = [
        'lat' => $input['lat'],
        'lng' => $input['lng'],
    ];

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

    return $input;
}

If you want more informations there is an article written by one of our engineer @julienbourdeau : GeoSearch

Give it a try, and tell me if it works for you.

@mikestratton
Copy link
Author

Hooray, it works! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants