【发布时间】:2020-01-16 16:35:09
【问题描述】:
我正在尝试修复的代码如下所示。我有一个 Hotel 类,用于在查询中获取某个区域内的所有酒店,但它不会丢弃那些不可用的酒店。里面有一个方法应该是一个访问器,但它没有按照我预期的方式编写:
public function isAvailableInRanges($start_date,$end_date){
$days = max(1,floor((strtotime($end_date) - strtotime($start_date)) / DAY_IN_SECONDS));
if($this->default_state)
{
$notAvailableDates = $this->hotelDateClass::query()->where([
['start_date','>=',$start_date],
['end_date','<=',$end_date],
['active','0']
])->count('id');
if($notAvailableDates) return false;
}else{
$availableDates = $this->hotelDateClass::query()->where([
['start_date','>=',$start_date],
['end_date','<=',$end_date],
['active','=',1]
])->count('id');
if($availableDates <= $days) return false;
}
// Check Order
$bookingInRanges = $this->bookingClass::getAcceptedBookingQuery($this->id,$this->type)->where([
['end_date','>=',$start_date],
['start_date','<=',$end_date],
])->count('id');
if($bookingInRanges){
return false;
}
return true;
}
我想使用此查询过滤掉酒店。所以这是来自控制器的查询:
$list = $model_hotel->with(['location','hasWishList','translations','termsByAttributeInListingPage'])->get();
是否可以将天数范围传递给函数?
顺便说一句,我尝试的第一件事是在查询之后使用集合并通过集合传递过滤器函数,然后手动分页,但虽然它确实过滤,但显然它丢失了 “Eloquent”结果集集合属性,它最终成为一个常规集合,因此它对我不起作用。
【问题讨论】: