laraloser's avatar

Morph in Many to Many relation

I have an address model. This model can be associated with other models, such as a building or an order. Each of these models can only have one associated address.

Typically, this type of relationship would be solved using a morph association, but in this case, an address can be associated with multiple houses or orders. What is the correct approach to solve this?

0 likes
1 reply
OussamaMater's avatar

I guess this is what you are looking for, if I understood you correctly

houses
    id - integer
 
orders
    id - integer
 
addresses
    id - integer
 
addressable
    address_id - integer
    addressable_id - integer
    addressable_type - string

So this way you can a single address associated with multiple houses and orders.

I highly recommend going through the example of the documentation stated above.

1 like

Please or to participate in this conversation.