'laravel polymorphic relationships with multiple assignments
On my Laravel project I have these models:
User
Technique (belongs to User)
TechniqueAd (belongs to Technique)
ConstructionObject (belongs to User)
ConstructionObjectAd (belongs to ConstructionObject)
Cargo (belongs to User)
CargoAd (belongs to Cargo)
Now I need to create another model - Deal, which holds deal properties, for example:
datetime (from)
datetime (to)
price
... etc.
But also it has to hold two models that are involved in the deal. For example, it can be Technique + ConstructionObjectAd or Object + TechniqueAd or Cargo + TechniqueAd, etc. What is the best way to achieve this? The ugly way would be to include these nullable fields:
technique_id
technique_ad_id
construction_object_id
construction_object_ad_id
cargo_id
cargo_ad_id
That references to coresponding models. Each of the database rows would always have only 2 id values filled in. But that seems very ugly... Is this the situation where I should use polymorphic relationships? For example, something like:
...
datetime (from)
datetime (to)
price
dealable_id
dealable_type
dealable_2_id
dealable_2_type
What is the best way to achieve a correct system structure for this scenario?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|