如何解决Laravel Algolia侦察兵,关系方面的人
我正在研究Laravel项目。我正在使用基于Algolia的Scout。但是我努力地在关系中使用whereIn。我有2种型号,如下。
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;
}
}
Category.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
我该怎么做?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。