我有车辆表。 (id, name, make)
我有vehicle_prices表(id, vehicle_id, mileage, price)
现在,可能在vehicle_prices
中,我可能多次具有相同的vehicle_id
,但它们的里程数却不同。因为车辆价格因里程而异。因此,我对vehicle_id
和里程具有唯一的索引。由于vehicle_prices
中有多个vehicle_id,我决定使用hasMany
关系。所以这是我正在做的事情。
$vehicle = App\Http\Models\Vehicle::with([
'prices' => function($query){
$query->where('milleage', 1000)->latest('id')->first();
})
])->where('id', 1)->first();
现在的事情是$ vehicle-> prices现在是一个数组,该数组中始终有1个对象。为什么它不仅是对象而是数组?我猜那是因为hasMany
的关系。
即使hasMany
可能在hasOne
中重复出现,我是否应该拥有vehicle_id
或将其更改为vehicle_prices
?