2

Simplify database with polymorphic many to many relations where there are multiple such relations in the same model. For example, the user model might have polymorphic relations to user_types, user_roles, user_statuses tables. Each of these tables might well have the same structure, but in the conventional many-to-many relationships with pivot tables, it would require seven tables to incorporate types, roles, and statuses. With a complex database, such as the one I'm working on, that issue rapidly expands into hundreds of tables. Since many people have difficulties understanding this issue, I've included a crude very simplified diagram below. In it you'll see that all four models on the right side have identical structures. To me, this seems totally needless, not to mention the repetition of pivot tables.Laravel 5 Polymorphic relationship with multiple relations per model

I'm new to Laravel so I may be missing the obvious. In everything that I've been able to find about Laravel polymorphic many to many relatons deals with only single such relations in any given model. The most common example given is the taggable situation. In this case, models such as Post, Article, and Review might each have one and only one relationship to the taggable model although they might each have multiple tags. I think I fully understand this, and it does not fit the situation I'm talking about.

What I'm talking about is the case where the Software model might have a polymorphic many to many relation to the Rubrics model for developmental stages (alpha, beta, release candidate, etc.) and for software types (text editor, messaging, database management, etc.). In this case, the Rubrics model would have to capture the "type" of relationship (software developmental stage vs. software type) in addition to model.

It seems like that this is a common enough issue that there must be some way to do this.

I'm hoping that someone will be able to provide a solution that simplifies the normal many-to-many relationship with a table for every single aspect — even though all such tables will likely have identical structures — along with the pivot/junction table for each such table.

LarryTX
  • 197
  • 2
  • 12

0 Answers0