i have a leaves table called user_leaves in which leaves taken by users are stored, table has this structure.
id, leave_type_id, from_date, to_date, status, created_at, updated_at.
leave_type_id is basically a foreign key of types of leaves my system has.
I want to fetch all leaves taken by a user in a range of two dates like start_date and end_date (for example all leaves from 2020-08-01 to 2020-08-31 .
I want records in numbers like,
{
"user_id" : 1,
"leaves" :
{
"medical_leave" : 2,
"earned_leave" : 5,
"sick_leave" : 3
}
}
I used the code
$UserLeaves = UserLeave::whereDate('date_from', '>=', $start_date)
->whereDate('date_from', '<=', $end_date)
->get();
The problem is that if i choose from_date to query my result ,and then loop through $UserLeaves to get all days between from_date and to_date for each leave_type.
Their are cases when to_date may exceed my end_date and thus giving wrong data if i calculate all days between from_date and to_date.