我在一家汽车租赁系统工作,所以我的计划是,当客户搜索所选车型中是否有可用的汽车时,首先:获取客户选择的日期的汽车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);才能获得除不可用汽车之外的所有具有关系的汽车
发布于 2019-01-24 05:32:48
如果您的关系设置正确,则可以使用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);https://stackoverflow.com/questions/54335857
复制相似问题