问题
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