问题描述
我正在研究Laravel项目。我正在使用基于Algolia的Scout。现在,我无法将“哪里有”应用于搜索。
我有一个名为Place的模型,该模型通过以下代码与Category有许多很多关系。
class Place extends Model
{
use Searchable,Localizable;
protected $with = [
'images','phones','emails','categories'
];
protected $casts = [
'is_featured' => 'boolean'
];
public function categories()
{
return $this->belongsToMany(Category::class,'place_category');
}
public function searchableAs()
{
return "places_index";
}
public function toSearchableArray()
{
$record = $this->toArray();
$record['_geoloc'] = [
'lat' => $record['latitude'],'lng' => $record['longitude'],];
unset($record['created_at'],$record['updated_at'],$record['latitude'],$record['longitude']);
return $record;
}
}
如您所见,它将在Algolia上建立索引。
我正在使用以下代码基于地理位置和关键字进行搜索。
Place::search($keyword,function ($algolia,$query,$options) use ($latitude,$longitude) {
$location = [
'aroundLatLng' => $latitude . ',' . $longitude,'aroundRadius' => config('scout.algolia.search_radius'),];
$options = array_merge($options,$location);
return $algolia->search($query,$options);
});
代码运行良好,直到我也尝试按类别过滤。我将代码更改为类似这样的内容。
$query = Place::search($keyword,$longitude) {
$location = [
'aroundLatLng' => $latitude . ',];
$options = array_merge($options,$location);
return $algolia->search($query,$options);
});
if ($category) {
$query = $query->whereHas('categories',function ($query) use ($category) {
$query->where('categories.id',$category);
});
}
您现在可以看到,我现在也使用whereHas按类别进行过滤。运行代码时,出现以下错误。
BadMethodCallException
Method Laravel\Scout\Builder::whereHas does not exist.
从字面上看,我不能在Algolia搜索中使用whereHas。我该如何解决?另外,我正在考虑索引类别并在Algolia一侧过滤记录,但是我将按ID进行过滤。如何为它自定义查询?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)