在我的Laravel模型中有以下代码:
public function toSearchableArray() {
return [
'name' => $this->name,
'description' => $this->description,
];
}
public function mappableAs(): array {
return [
'name' => 'text',
'description' => 'text',
];
}
public function makeAllSearchableUsing($query) {
return $query->with('users');
}
public function users() {
return $this->belongsToMany(User::class, 'users');
}
我正在使用Explorer与Laravel Scout,我已经刷新并导入了数据。
当我运行查询Model::search('fish')->field('name')->get()
时,输出仅包含以下数据:
id: 1,
name: "Ea nemo fish unde esse",
description: "Tempore nihil cumque eum quo"
但是,我希望得到的用户关系数据如下所示:
users: {
{id: 1, name: 'User1'},
{id: 2, name: 'User2'},
}
或者添加$query->withCount('users');
的关系并按其排序。
是什么导致了这个问题?
这是我的用户模型
public function toSearchableArray(): array {
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
public function mappableAs(): array {
return [
'id' => 'integer',
'name' => 'text',
'email' => 'text',
];
}
public function models($owner = true) {
return $this->hasMany(Model::class);
}
我的explorer.php
'indexes' => [
Model::class,
User::class,
],
1条答案
按热度按时间kulphzqa1#
您必须在
toSearchableArray
方法中包含users
关系: