laravel scout 增加附加属性(仅限美力搜索)

laravel scout add additional attribute (only in meilisearch)

我尝试使用 laravel scout 将基于 SQL 的搜索迁移到 meilisearch。

目前整个搜索应该迁移到美力搜索,包括所有筛选和排序选项。

一个productfeedbacks(产品型号)有关系:

//returns all feedbacks for the product
public function allFeedbacks()
{
    return $this->hasMany('App\Models\Feedback');
}

我想包括对美力搜索的反馈数量,但不是整个关系,因为排序不需要它。

如何在不将字段添加到 mySQL 数据库的情况下添加其他字段以供美力搜索索引 (feedback_amount f.e.)?

Product.php中:

public function toSearchableArray() {
  $fields = [
    'feedback_amount' => $this->allFeedbacks()->count(),
    'price' => $this->price,
    'other_stuff' => $this->other_stuff
  ];

  return $fields;
}

然后刷新并再次导入模型。

这将覆盖父级 toSearchableArray(),因此您将希望包括任何其他您希望可搜索的字段。