Are Laravel's Polymorphic Relations also suitable for true polymorphism?

余生长醉 提交于 2019-12-23 19:16:35

问题


To explain myself better: the examples on Laravel.com showcase a relation of comments belonging to both videos and posts. However I'm talking about specification: not belonging to, but being a specification of the parent table. (Subtyping) Is using Laravel's Polymorphic Relations still the best approach?

A very basic (and potentially bad) example.


回答1:


Interesting question.

It is entirely possible to do what you require, with the polymorphic relation that Laravel provides. You however, are referring to table inheritance which is whole other kettle of fish.

You could have your Animal model have a polymorphic relationship that can either be Fish or Mamal, or you could have Fish and Mamal belong to an Animal, and create a pass-through model.

It really depends exactly on how you'll be using this. Will you be going through Animal as in querying the animals table, or will you be going from the children?



来源:https://stackoverflow.com/questions/49486341/are-laravels-polymorphic-relations-also-suitable-for-true-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!