1

我有一个Tab模型,它与包括我的模型在内的许多其他模型具有一对多的多态关系User。我将这种关系称为关系customizers(因为它们自定义选项卡的内容)并将它们存储在一个名为与tab_customizers关系相关列的表中。因为这种关系也可以具有相同模型的不同类型,所以我包含了另一个名为.customizer_idcustomizer_typerelation_type

例如,在 myTabUsermodels 之间,有 2 种不同的关系:

  • AUser可以与 a相关Tabauthor如果相关用户写了帖子,它将显示在选项卡中)
  • AUser可以关联到 aTab作为其焦点mention(如果相关用户在帖子中提及,它将显示在选项卡中)

这是我在Tab模型中定义的关系:

public function authors()
{
    return $this->morphedByMany(User::class, 'customizer', 'tab_customizers')->where('relation_type', 'author');
}

public function mentions()
{
    return $this->morphedByMany(User::class, 'customizer', 'tab_customizers')->where('relation_type', 'mention');
}

这是我Tab资源中的 Nova 字段:

MorphedByMany::make('authors', User::class),

MorphedByMany::make('mentions', User::class),

这是我的问题:当我选择一个User模型将其附加到mentions(实际上是customizerswith mentionas its relation_type)时,它会附加authoras relation_type。如何让 Nova 区分这两种类型?

此外,我不能同时在TabandUserauthorasrelation_type和另一个与mentionas之间建立关系relation_type。当我尝试(使用修补程序)附加其中一种类型的关系而另一种类型的关系已经存在时,它将更新现有关系并且不附加新关系。

4

0 回答 0