Skip to main content

Laravel.io: Polymorphic All the Things!

Premium
2:58

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

P
Pēteris ✓ Link copied!

Thank you for the video and explanation. I was wondering - what is advantage of using polymorphic vs many tables (for instance, in case of tbl_likes)? Is it that we can put all the data in one table (tbl_likes) and not it many (tbl_likes_auhtors, tbl_likes_for_articles, etc.)? How does it affect query performance as with time such a table would be huge? Or still, - this way is the best way for saving space in DB?

PK
Povilas Korop ✓ Link copied!

From me experience, it's more about FLEXIBILITY than performance. So, one table with potentially adding more data types, without changing the DB structure.

Yes, it is likely to be slower in the long run, with a lot of data, so if you care more about performance than flexibility and reusability of the code, then duplicating data (and all the code around it - Models, etc) instead of polymorphic relations is your preferred option.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.