Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chandy's avatar

Check if leave application exists within dates

I have leaves table which has employee_id, leave_type_id, from_date , to_date etc. I also have a form that captures the from_date and to_date , so i wanted to do a check that if an employee applies for a leave that has its from_date and to_date between the from_date and to_date existing in the database to have the application declined.

this is what i have tried so far but it is not accurate:

 $leave_exists = Leave::where('employee_id',$request->employee_id)
                ->whereBetween(DB::raw('from_date AND to_date'), [$request->from_date, $request->to_date])
                ->get(); 
0 likes
4 replies
tisuchi's avatar

I don't think so the whereBetween is the right way to solve your issue since your db structure is bit different.

In that case, you may try this-

$leave_exists = Leave::where('employee_id',$request->employee_id)
    ->where('from_date', '>=', $request->from_date)
    ->where('to_date', '<=', $request->to_date)
    ->get();
4 likes
Snapey's avatar
Snapey
Best Answer
Level 122

You also have to accomodate the case where the previous request completely encompases the new leave booking.

something like (obviously not tested)

 $leave_exists = Leave::where('employee_id',$request->employee_id)
        ->whereBetween('from_date',[$request->from_date, $request->to_date])
        ->orWhereBetween('to_date',[$request->from_date, $request->to_date])
        ->orWhere(function($query) use($request){
            $query->where('from_date','<=',$request->from_date)
                ->where('to_date','>=',$request->to_date)
        })->first();
3 likes
wakay2022's avatar

@Snapey Hi, where did you get the $request->from_date?How did you do that? Thank you!

Please or to participate in this conversation.