我正在为汽车租赁预订系统工作 所以我的计划是当客户搜索所选车型中是否有可用汽车时, 首先:获取客户选择的日期已占用的汽车 ID 第二:获取除不可用汽车之外的所有汽车,
$collection = Booking::whereDate('dropoffday', '>' ,$date1)
->whereDate('pickupday' ,'<', $date2)
->get(['car_id'])
;
if ($collection->isEmpty()) {
$cars = Car::with('marques.images')->get();
return Response::json($cars);
}
$taken = [];
foreach ($collection as $car) {
$id = $car->car_id;
array_push($taken,$id);
}
$cars = Car::with('marque.images')->except($taken);
return Response::json($cars);
}
我该如何重写这一行 $cars = Car::with('marque.images')-> except($available); 获取除不可用汽车之外的所有有关系的汽车
最佳答案
如果您的关系设置正确,您可能可以使用 whereDoesntHave
方法,如下所示:
$cars = Car::with('marque.images')->whereDoesntHave('bookings', function ($query) use ($date1, $date2) {
$query->whereDate('dropoffday', '>' ,$date1)
->whereDate('pickupday' ,'<', $date2);
})->get();
return Response::json($cars);
关于php - 拉维尔 |检索具有关系且特定 ID 除外的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54335857/