How about you use one single Model for all of your vehicles and then use QueryScope for every single type of vehicles.
Examples :
- Fetching all vehicles
Vehicle::all();
- Cars :
Vehicle::cars()->get();
and so on...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I am doing some experiments with my project's database and I came across with a tricky problem. Think that I have a table called "vehicles" where I can different types of vehicles like cars, motorcycles or trucks. Eloquent expects a single model capable of managing this table. However, in this case, I would like to have a Model for each type of vehicle which would extend the VehicleModel. For singles queries to DB where I want a specific type of vehicle I don't see any problem. For example, I can load a CarModel from DB with this call:
CarModel::find($carModelId)
Unfortunately, when I need to fetch all the items, I have to use the parent class, i.e., the VehicleModel.
VehicleModel::all();
This returns a collection of VehicleModels. I would like to have a collection of CarModels, MotorcycleModels and TruckModels. How can I do this? Does the Eloquent reserve any solution like some kind of factory pattern to this?
Please or to participate in this conversation.