Json 尝试保存新记录时发生地理位置错误
我正在使用algolia和laravel进行搜索。这很好,但当我添加地理定位时,我遇到了一个问题。我在数据库中有lat和lng字段,因此将其添加到模型中:Json 尝试保存新记录时发生地理位置错误,json,laravel,algolia,Json,Laravel,Algolia,我正在使用algolia和laravel进行搜索。这很好,但当我添加地理定位时,我遇到了一个问题。我在数据库中有lat和lng字段,因此将其添加到模型中: public function toSearchableArray() { $record = $this->toArray(); $record['_geoloc'] = [ 'lat' => $record['latitude'], 'l
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;
}
//gets an array of lat and lng
//$gps is an assoc 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,
]);
但是,当我试图保存模型时,出现以下错误:
_geoloc.lat或_geoloc.lng属性在第1行>第526列附近不能为空
然后我在数据库中添加了一个“_geoloc”字段,这是一个文本。
但现在,我在尝试保存模型时遇到以下错误:
AlgoliaSearch\AlgoliaException(400)
_geoloc属性不能在第1行第606列附近包含字符串
从错误消息中:
$answer=Json::decode($response,true)
以下是模型的保存:
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;
}
//gets an array of lat and lng
//$gps is an assoc 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,
]);
我是从一开始就做错了,还是在最后做错了?使用toSearchAbleArray,algolia代码不应该能够解码数据库中保存的json字符串吗?提前谢谢 您对
ToSearchBearRay
是执行此操作的写入方式,但模型/控制器中的关联数组键不匹配。在控制器中,您使用的是“lat”和“lng”,但在模型中,您使用的是“纬度”和“经度”
在模型更改中:
'lat'=>$record['latitude'],
“lng”=>$record[“经度”],
致:
'lat'=>$record['lat'],
'lat'=>$record['lng'],
您将无法在MySQL中存储关联数组。我建议改变:
$gps=Map::getListingDetails($request);
到
$input=Map::getListingDetails($request)
$gps=序列化($input);