2 weeks ago

Double polymorphic relation, anyone?

Posted 2 weeks ago by jinelcrimp8

So, I said that I should try and make a small browser game just to get some advanced laravel concepts under my belt (and honestly because, trying new things in a corporate context is boring). So the first thing that I would do is create a user model and a monster model. A user can own a monster, simple. But a user can own some monies, some weapons and such....

So I though I could make a trait that will be called "isPropertyTrait" and will make that thing ownable. Now because in my design I want virtually everything to be ownable by something else (a monster can have some treasure, the treasure can have some curse, the whole lot!), I figured that this thing can be achieved by some polymorphic relation of some sort. Maybe a double relation [owner_id, owner_type, property_id, property_type].

And this is where it is getting confusing. Because the models that have a polymorphic relation have some extra columns in their tables, and I dont want to repeat the same columns across all my models.

Is there a way to achieve this? How would you go about it?

Please sign in or create an account to participate in this conversation.