代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

Laravel Algolia Scout,其中关系

  •  0
  • Wai Yan Hein  · 技术社区  · 4 年前

    我正在做一个Laravel项目。我使用的是基于Algolia的Scout。但我很难将其应用于人际关系中。我有以下两种型号。

    Place.php

    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'],
            ];
            $record['categories'] = $this->categories->map(function ($data) {
                return [
                    'id' => $data['id'],
                    'en_name' => $data['en_name'],
                    'mm_name' => $data['mm_name'],
                ];
            })->toArray();
            unset($record['created_at'], $record['updated_at'], $record['latitude'], $record['longitude']);
            unset($record['images'], $record['phones'], $record['emails']);
    
            return $record;
        }
    }
    

    类别.php

    class Category extends Model
    {
        use Searchable;
    
        protected $touches = [
            'places',
        ];
    
        public function places()
        {
            return $this->belongsToMany(Place::class, 'place_category');
        }
    }
    

    现在,我正在按类别搜索Place模型/数据过滤。如您所见,我还在toSearchableArray方法中用位置对类别进行了索引。

    我正在努力实现这样的目标。

    Place::search($keyword)->whereIn('categories', ????);//how can I filter by the Ids here
    

    我该怎么做?

    0 回复  |  直到 4 年前