Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

diegopr83's avatar

Multiple Polymorphic relationship in one model

Hi everybody, i need help with this situation:

I have three tables:

products: -id -description

batches -id -description

and i need a third one:

exchanges: -id -exchange1 -exchange2

These exchange1 and exchange2 could be a product or a batch, so these need to be polymorphic.

but i don't know how to insert a new row using eloquent.

please help!!

0 likes
10 replies
diegopr83's avatar

thanks for the replies.

I know how to manage a polymorphic relationship. But this time i need two polymorphic fields.

Exchange1 would become to a exchange1_type and enchage1_id

Exchange2 would become to a exchange2_type and enchage2_id

But how do i configure the relationships to insert using eloquent?

Vilfago's avatar

What is exchange in your context ? What do you want to achieve ?

I got the impression that you over-complicate something.

diegopr83's avatar

i need to save a relation between two products or batches (an exchange or swap) but both id's on the table could be a product or a batch so i need both id's to be polymorphic

do i explain myself?

Vilfago's avatar

@DIEGOPR83 - I don't know, but I always see that product have batches, and you only link batch.

But a product can have one or many batch, it's not an issue.

So Product hasMany batches Batch belongsTo Product batch belongsToMany batches (many to many relations)

diegopr83's avatar

@vilfago i'll try to change the context to try to explain my situation:

Table 1) teams Table 2) National teams

and we have a third table with matches. So one team can play with another team or with a national team. (just imagine)

Table 3)

  • id
  • competitor1
  • competitor2

so both of them (competitors need to be polymorphic 'cause they could be a team or a national team)

so the table 3 would be like this:

  • id
  • competitor1_id
  • competitor1_type
  • competitor2_id
  • competitor2_type

now, i have to set it in the elequent mode and try to insert a row but i dont know how

josefelipetto's avatar

@diegopr83 , i don't think there is a way of doing that using eloquent built in polymorphics relations, because it's defined by just two fields, id and type.

You have two options:

  1. Create your custom polymorphic relation class, then you would have to handle all the details of Insert, Update, Find,etc

  2. Since Teams and National Teams are both teams, you could create a master table for teams, and then two tables(table 1 and table 2) that references that master table. It's like inheritance but in the database model. In that way you would have just one ID that you can use the find method, and also a type field that you will use to get the proper relation

josefelipetto's avatar

@vilfago yes, i kinda like it, i think it gives your app some flexibility and you don't have to manage tons of tables, all you need to do is specify the model it belongs. It may causes perfomance issues, since you're not really creating a foreign key, thus a index that could speed up queries, but for the most apps nowadays, this is not a problem. But in this case, the built in polymorphics relation won't work, so yeah, solution 2 might be more appropriate

Please or to participate in this conversation.