I have vehicles table. (id, name, make)
I have vehicle_prices table (id, vehicle_id, mileage, price)
Now, It's possible that in vehicle_prices, i might have the same vehicle_id multiple times, but all of them with different mileage. Because vehicle prices differ depending on mileage. So I have unique index on vehicle_id and mileage. Because of that multiple vehicle_id in vehicle_prices, I decided to use hasMany relationship. So here is something I am doing.
$vehicle = App\Http\Models\Vehicle::with([
'prices' => function($query){
$query->where('milleage', 1000)->latest('id')->first();
})
])->where('id', 1)->first();
The thing now is that $vehicle->prices now is an array which will always have 1 object in there. why is it not just an object instead of array? I guess that's because of hasMany relationship.
Should I have hasMany or change it to hasOne even though vehicle_id might get repeated in vehicle_prices?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire