3 回答

TA貢獻2003條經驗 獲得超2個贊
好的,我已經找到了解決方案,問題出在遷移中,您必須使用對象才能索引 belongToMany 這樣的關系
Index::create('stages', function (Mapping $mapping, Settings $settings) {
$mapping->text('intitule_stage');
$mapping->text('objectifs');
$mapping->text('contenu');
$mapping->object('mots_cles');
});
在您的模型中:
public function toSearchableArray()
{
return [
'intitule_stage' => $this->intitule_stage,
'objectifs' => $this->objectifs,
'contenu' => $this->contenu,
'n_stage' => $this->n_stage,
'mots_cles' => $this->motsCles()->get(),
];
}
結果現在正如預期的那樣

TA貢獻1805條經驗 獲得超9個贊
與 belontoMany 關系存在相同的問題,并且為了將關系作為嵌套對象,我做了同樣的事情,但是當我嘗試填充我的索引時,字段“mots_cles”保持為空,我不明白為什么。
這是遷移:
Index::create('stages', function (Mapping $mapping, Settings $settings) {
$mapping->text('intitule_stage');
$mapping->text('objectifs');
$mapping->text('contenu');
$mapping->nested('motsCles', [
'properties' => [
'mot_cle' => [
'type' => 'keyword',
],
],
]);
});
模型:
public function toSearchableArray()
{
return [
'intitule_stage' => $this->intitule_stage,
'objectifs' => $this->objectifs,
'contenu' => $this->contenu,
'n_stage' => $this->n_stage,
'mots_cles' => $this->motsCles(),
];
}
public function motsCles()
{
return $this->belongsToMany(MotsCle::class);
}

TA貢獻1820條經驗 獲得超2個贊
如果你想得到分類的“nom”,把它寫在 composant Model 中
'categorie' => $this->categorie->nom ?? null,
$this->categorie() 返回關系,而不是對象。
- 3 回答
- 0 關注
- 167 瀏覽
添加回答
舉報