@wakil ahmed please format your code correctly by wrapping it within triple backticks
```
// your code
```
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
The following function is called 3 times from a controller with different parameters. Each time 30/31 queries are running according to the dates in a month. In the index page this single function is making 90-93 queries in a single page. How can I optimize it?
Here is the function:
function attendanceData($dates, $status_id, $department)
{
$attendanceresult = [];
foreach($dates as $date)
{
$presents = Attendance::with('Employee', function($q) use($department)
{
$q->where('department_id', '=', $department)
->where('designation_id', '!=', 26);
})
->whereDate('in_time', $date)
->whereIn('attendance_status_id', $status_id)
->count();
if($presents)
{
$attendanceresult[] = $presents;
}else
{
$attendanceresult[] = 0;
}
}
return $attendanceresult;
}
The function is called with the following parameters:
$absentresult = attendanceData($dates, [4], $department = false);
$totalPresentResult = attendanceData($dates, [1, 2, 3, 5], $department = 3);
$totalAbsentResult = attendanceData($dates, [4], $department = 3);
One more thing to note here is I need the data in an array with each days data in an individual key => value pair. Like:
array:10 [
0 => 7
1 => 0
2 => 7
3 => 8
4 => 3
5 => 12
6 => 13
7 => 12
8 => 0
9 => 12
]
Please or to participate in this conversation.