Set foreign keys to reference the vin number, then set up the Eloquent 'belongsTo' relationships, and when you make the call you can do
$car = Cars::with(['photos', 'features', 'favorites']->etc...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a static database that contains information on used cars. There are 4 tables
Table 1: usedCar // general info about car (100,000 rows)
Table 2: usedCarFeatures // list of features available with car (2,000,000 rows)
Table 3: usedCarPhotos // list of photos for each car (20,000,000 rows)
Table 4: usedCarFavorites // people who favorited the car (1,000,000 rows)
The VIN number is stored in tables 2,3, and 4
when a user conducts a search, our app connects with an API to get the VIN number of the vehicle that best matches their query
foreach($vinNumbers as $vin){
$car = Cars::where('vin', $vin)->first();
$photos = CarPhotos::where('vin', $vin)->get();
$features = CarFeatures::where('vin', $vin)->get();
$favorites = CarFavorites::where('vin', $vin)->get();
}
I believe the above process is inefficient and wastes a lot of resources. How can I do a join across all 4 databases via the VIN while maintaining the simplicity of the above? I love the fact that I can through the photos, features, and favorites variables into a loop and manipulate as I please.
Please or to participate in this conversation.