如何解决无法使用Laravel Algolia Scout中的wheres
我正在研究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 举报,一经查实,本站将立刻删除。