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

hromby's avatar

Eloquent and parent-child relations

Hello!

In my current project, I have three models: Vehicle, Car and Truck. In a traditional OO approach, the Car and Truck classes would extend Vehicle, which would be an abstract base class. My problem is: how do I structure that with Eloquent?

One approach might be to have three tables; one for each model. The vehicles table could have a polymorphic relation which points to the right child object in either cars or trucks. These tables would then correspond to a model each, with Vehicle having a morphTo relation to the subclass, and the subclasses would have a morphOne back to the base class, so no real abstract class and subclasses here -- but I think it could be done in a cleaner way -- but I dont know how :-)

How would you structure it? Preferably, I'd like to be able to add a whole new subtype of vehicles without tinkering too much with the existing setup. Also, I would like to have a simple way of listing all vehicles regardless of subtype in a single list.

0 likes
0 replies

Please or to participate in this conversation.