Я думаю, все, что вам нужно, это whereBetween()
и orWhereBetween()
$keyWithData = DB::table('leave_allocations')
->select('employee','leave_type','name')
->where(function($query) use($dateS, $dateE) {
$query->whereBetween('from_date', [
$dateS->format('m-d-Y 00:00:00'),
$dateE->format('m-d-Y 00:00:00')
])
->orWhereBetween('to_date', [
$dateS->format('m-d-Y 00:00:00'),
$dateE->format('m-d-Y 00:00:00')
]);
})
->where([
['leave_allocations.employee',$empdata->name].
['leave_allocations.leave_type',$type]
])
->first();
Другой подход с использованием диапазона дат
// create a period object between new leaves start date and end date.
$period = new \DatePeriod(
$dateS,
new \DateInterval('P1D'),
$dateE
);
// all the days between new leave start date and end date.
$new_leave_dates = [];
foreach ($period as $key => $value) {
array_push($dates, $value->format('Y-m-d'));
}
// get all the rows without those days.
$keyWithData = DB::table('leave_allocations')
->select('employee','leave_type','name')
->whereNotIn('from_date', $new_leave_dates)
->where([
['leave_allocations.employee',$empdata->name].
['leave_allocations.leave_type',$type]
])
->first();