Иногда с помощью этих шагов можно избежать множественных условных запросов.
- Сохранить результат запроса в переменной. Результатом является коллекция.
- Используйте where и другие помощники коллекции для ограничения коллекции.
Единственный запрос и помощники коллекции могут сделать трюк.
Так я и сделал.
Запрос
$appointment_at = $this->appointment_at;
if(request()->ismethod("Post") || request()->ismethod("Patch")) {
$schedules = Schedule::whereHas('dayTimes', function ($query) use ($appointment_at) {
$query->where('start_time', '<=', $appointment_at->format('H:m'))
->where('end_time', '>=', $appointment_at->format('H:m'));
})
->whereDate('start_date', '<=', $appointment_at)
->whereDate('end_date', '>=', $appointment_at)
->get();
}
Использование помощников по сбору
if(empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules;
}
else if(!empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('service_id', $this->service_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('service_id');
}
}
else if(empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('user_id');
}
}
else if(!empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(!empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('service_id', $this->service_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('service_id', $this->service_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('service_id');
}
}
else if(!empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id')->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}