I have a Tab
model which has a one-to-many polymorphic relationship with many other models including my User
model. I've called this relationship customizers
(because they customize a tab's content) and store them in a table called tab_customizers
with customizer_id
and customizer_type
as relation-related columns. Because this relation also can have different types with the same model, I've included another column called relation_type
.
For example, between my Tab
and User
models, there are 2 different relations:
- A
User
can relate to aTab
as itsauthor
(if the related user write a post, it'll be displayed in the tab) - A
User
can relate to aTab
as its focusedmention
(if the related user is mentioned in a post, it'll be displayed in the tab)
Here are my relations defined in the Tab
model:
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');
}
And here are my Nova fields in my Tab
resource:
MorphedByMany::make('authors', User::class),
MorphedByMany::make('mentions', User::class),
Here is my issue: When I select a User
model to attach it to mentions
(actually to customizers
with mention
as its relation_type
), it'll attach with author
as relation_type
. How can I make Nova distinguish between these 2 types?
Besides, I can't have a relation between Tab
and User
with author
as relation_type
and another with mention
as relation_type
at the same time. When I try (using tinker) to attach a relation of one of the types while a relation of the other type already exists, it will update the existing relationship and does not attach a new one.