I had a situation come up where I needed an Eloquent model to hydrate dynamically into a specific class that extends my "normal" eloquent model. Here's a generic layout loosely based on typical electronics:
Data Object:
['id', 'model_type', 'name', 'serial',...]
- Model
- Model Type A
- Model Type B
If the data is all in a single table (MySQL), Laravel doesn't really have a way to pull that data directly as a polymorph. You can polymorph a relationship, but not a model directly.
Essentially the reasoning for this is to separate logic that may be concerned with one type, but not another. For example, Model Type A
and Model Type B
can both implement an interface which describes their capabilities, but the specific logic for A doesn't need to pollute B.