Skip to content

Commit a036c07

Browse files
committed
Cleaner where clauses in between scope.
1 parent 70ea48d commit a036c07

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

app/Models/Booking.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,22 @@ public function scopeBetween(Builder $query, $start, $end): Builder
213213
$endTime = $end->copy()->subSecond();
214214

215215
// Return any bookings with the same resource and within the same time frame.
216-
return $query->where(function ($query) use ($startTime, $endTime) {
217-
$query
218-
// Exactly the same time as given interval.
219-
->where(function ($query) use ($startTime, $endTime) {
220-
$query
221-
->where('start_time', '=', $startTime->copy()->subSecond())
222-
->where('end_time', '=', $endTime->copy()->addSecond());
223-
})
224-
// Start before and end after interval.
225-
->orWhere(function ($query) use ($startTime, $endTime) {
226-
$query
227-
->where('start_time', '<', $startTime)
228-
->where('end_time', '>', $endTime);
229-
})
230-
// Start in interval.
231-
->orWhereBetween('start_time', [$startTime, $endTime])
232-
// End in interval.
233-
->orWhereBetween('end_time', [$startTime, $endTime]);
234-
});
216+
return $query->where(fn($query) => $query
217+
// Exactly the same time as given interval.
218+
->where(fn($query) => $query
219+
->where('start_time', '=', $start)
220+
->where('end_time', '=', $end)
221+
)
222+
// Start before and end after interval.
223+
->orWhere(fn($query) => $query
224+
->where('start_time', '<', $startTime)
225+
->where('end_time', '>', $endTime)
226+
)
227+
// Start in interval.
228+
->orWhereBetween('start_time', [$startTime, $endTime])
229+
// End in interval.
230+
->orWhereBetween('end_time', [$startTime, $endTime])
231+
);
235232
}
236233

237234
/**

0 commit comments

Comments
 (0)