我有vehicles
模型,car_id
, year
, license_plate
字段。
我有Car
模型与car_class_id
, car_brand_id
, name
字段。汽车名称包含类似(思域,帕杰罗等)
我有Car_brands
与name
字段。(本田、丰田)
我有Car_classes
与name
字段。(SUV、MPV、Truck等)
vehicles
模型中的car_id
属于Car
模型
car_class_id
属于car_classes
, car_brand_id
属于car_brands
我怎么能得到车辆列表在哪里有car_class名称MPV?
我的关系是好的设计还是我只是把汽车品牌名称和汽车类别名称放在车辆表上?
您可以尝试如下:
Vehicle::whereHas('car' , function($q) {
$q->whereHas('car_classes' ,fucntion($q) {
$q->where('name', 'MPV');
});
})->get();
解决方案必须是这样的
Vehicle::whereHas('car' , function($query) {
$query->whereHas('car_classes' ,function($query2) {
$query2->where('name', 'MPV');
});
})->get();