我有一个Tab
模型,它与包括我的模型在内的许多其他模型具有一对多的多态关系User
。我将这种关系称为关系customizers
(因为它们自定义选项卡的内容)并将它们存储在一个名为与tab_customizers
关系相关列的表中。因为这种关系也可以具有相同模型的不同类型,所以我包含了另一个名为.customizer_id
customizer_type
relation_type
例如,在 myTab
和User
models 之间,有 2 种不同的关系:
- A
User
可以与 a相关Tab
(author
如果相关用户写了帖子,它将显示在选项卡中) - A
User
可以关联到 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
(实际上是customizers
with mention
as its relation_type
)时,它会附加author
as relation_type
。如何让 Nova 区分这两种类型?
此外,我不能同时在Tab
andUser
与author
asrelation_type
和另一个与mention
as之间建立关系relation_type
。当我尝试(使用修补程序)附加其中一种类型的关系而另一种类型的关系已经存在时,它将更新现有关系并且不附加新关系。