无法使用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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...