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

davorminchorov's avatar

How To Use Polymorphic Relationships / Associations!

Hello!

I've been searching for Polymorphic Relationships / Associations and it seems there's hardly any resources on the subject. Even the documentation is not really detailed on this (a more detailed examples + how to use them would be nice).

So, I am creating this discussion so we can have it as a resource where people can link useful blog articles, tutorials and videos and even share some experience / knowledge on this topic.

Anyone here has any experience with them so they can share some on how we can use:

  • One To One
  • One To Many
  • Many to Many

polymorphic relationships?

Some theory on it and when to + how to use them would be nice. (insert, update, delete, some other useful methods etc.)

Videos:

Blog Articles:

0 likes
18 replies
Ricardo's avatar

I think you should edit your first post and collect there the relevant info, so one no need to search over the whole thread to find the info needed.

davorminchorov's avatar

Updated my original post with the videos, also I added the Polymorphic Huh? lesson.

2 likes
mehany's avatar

I ld love to see some advanced videos on this topic as well. I am familiar with Polymorphism from JAVA and it is, yet another approach to, a good software design. It is actually a fundamental principal of OOP, and I think it is at the heart of Laravel framework itself.

Some theory on it...

Polymorphism provides a means to create elegant versatility in our software. It allows us to apply a consistent approach to inconsistent but related behavior. We should try to find opportunities in our software systems that lend themselves to polymorphic solutions. We should seek them out, actively and deliberately, before we begin to write code. ( Source )

Whenever you find situations in which different types of objects perform the same type of behavior, there is an opportunity for a polymorphic solution. The more experience you get, the easier it will be to detect such situations.

Here are some situations where polymorphic relationships works best:

  • All business transactions must be logged. ( loggable )
  • A drawing program allows the user to draw various shapes. ( drawable )
  • An Object to Array conversion. ( arrayable )
1 like
jekinney's avatar

I use them sometimes. Imo it replaces a pivot table, and In some cases multiple pivots.

But the draw back can be coupled code and a huge table that can slow response time considerably. Take for example, though can be crazy, gets my point across.

Your app has a discussion board that users comment on a post. A blog that users can comment on a article. You could just have one comment table that utilizes a polymorphic relationship. But as you could imagine that table and model becomes tightly coupled to both features or a feature as it's self with dependencies. Also imaging who big that table could get.

Now as the docs point out photos, that could also get bloated if not carefully constructed.

1 like
davorminchorov's avatar

Interesting reply, @jekinney ! I've read in another thread (I was asking about pros and cons there) that they might cause performance issues but if the right fields are indexed properly, it won't be that big of a deal! Not really a database and performance pro but I have to investigate this further.

jekinney's avatar

@Ruffles thank you, more personal preference I think then as you stated performance per say. I just imagine then experience that like I sated a comment table could end up with a million rows where the other tables might have a few thousand each.

Please or to participate in this conversation.