3 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
好的,我已經(jīng)找到了解決方案,問(wèn)題出在遷移中,您必須使用對(duì)象才能索引 belongToMany 這樣的關(guān)系
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(),
];
}
結(jié)果現(xiàn)在正如預(yù)期的那樣

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
與 belontoMany 關(guān)系存在相同的問(wèn)題,并且為了將關(guān)系作為嵌套對(duì)象,我做了同樣的事情,但是當(dāng)我嘗試填充我的索引時(shí),字段“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貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果你想得到分類(lèi)的“nom”,把它寫(xiě)在 composant Model 中
'categorie' => $this->categorie->nom ?? null,
$this->categorie() 返回關(guān)系,而不是對(duì)象。
- 3 回答
- 0 關(guān)注
- 184 瀏覽
添加回答
舉報(bào)