我想查看一名员工整个月的日程安排。目前,我正在迭代一个月的每个日期,然后是该特定日期的每个小时。当我看到调试器时,我得到了1000多个查询。是否有任何优化的方式从员工表中获取所有调度,存储在一个变量中,然后通过该数组检查我的条件,而不是每次击中服务器并从db获取数据
#iterating each day for whole month
foreach ($dates as $key => $today) {
$daily = []; $daily[0] = 'fullyAvailable';
$i = 1;
#iterating each hours, 17 hours a day (17 iteration)
for ($time = $opening_time; $time <= $closing_time; $time++) {
$unit = $inst->instructor->hourlySubscribedUnit($today, date("H:00:00", mktime($time)), date("H:00:00", mktime($time + 1)));
if (isset($unit)) {
$daily[0] = 'fullyScheduled';
$daily[$i] = $unit->unit_type;
$totalScheduledCount++;
}elseif ($inst->instructor->timings($today)->count() > 0) {
$daily[0] = 'fullyAbsent';
break;
} else {
$daily[$i] = 'available';
$totalAvailableCount++;
}
$i++;
}
$monthly[$key] = $daily;
}
#and this is the relationship defined inside the staff model
{
return $this->hasMany(SubscriptionUnits::class, 'instructor_id')
->whereDate('date', $date)
->when($from && $to, function($query) use ($from, $to){
return $query->whereTime('from', '>=', $from)->whereTime('to', '<=', $to);
})->select('unit_type')->first();
}`
我认为你应该做的是急于加载员工。您可以在laravel文档
中查看如何执行此操作。