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

CrucialDev's avatar

Polymorphic Relationship Use Case

So I am working on building a content repository that will contain both "authored" content (through a tinyMCE editor) and also uploaded content (.pdf, .xlsx, .docx. The two content types have their own models but I want to return view that contains a table that lists both content types. Before I dig into the polymorphic relationship path, would this be a good use case? Would I be able to get the desired table "mashup" of the two types?

0 likes
1 reply
jekinney's avatar

Yes you could. Polymorphism imo is a connection table (pivot table) on steroids. The examples in the docs use photos and likes. They are a perfect example for a basic but common use case. Instead of, say a blog having photos and maybe a user also having a photo gallery and multiple models essentially doing the same thing you can use one model for all photos. Now where this might not always be the answer is the photo model with polymorphism can be tightly coupled to your application desgin. Meaning if your blog is more of a modular desgin like a plugin, your assuming that every app you use your blog code also has to have a photo model with polymorphism. Which maybe you don't need in every application you need a blog in.

I try to utilize polymorphism like you described if possible as the specific model might be only tied to that instance and not nessesary globally if that makes sense.

Please or to participate in this conversation.