我正在修复一个项目,并在模型中创建了一个范围来查询也执行闭包的关系:
这是范围,这是一家酒店:
public function scopeIsHotelAvailable($query, $start_date, $end_date){
return $query->whereHas('isAvailableInRanges', function($q) use ($start_date, $end_date) {
$q->isAvailableInRanges($start_date, $end_date);
});
}
当我什至尝试运行它时,我收到以下错误:
$hotel->ishotelavailable($start, $end);
TypeError: Too few arguments to function Modules/Hotel/Models/Hotel::isAvailableInRanges(), 0 passed in /home/ffuentes/pk2/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php on line 475 and exactly 2 expected
我以前见过这个,但我不知道如何让 Eloquent 识别这些论点。
编辑:
这是从作为模型 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;
}
所有这一切的重点是针对这些结果测试查询结果并过滤它们。那时当我开始尝试它时,我只是过滤了集合,但它不起作用,因为返回的集合不包括分页和它通常在自身内部携带的雄辩元素。