I have this code. Finally, the player is returned with the levelDifference attribute for every day 7 days back. Could this be optimized to one sql query instead of looping and doing 7 queries?
Also, I would like to know if you can omit withMin and withMax and just get the difference between the smallest and the largest lvl on a given day?
$days = collect();
foreach (range(1, 7) as $dayNumber) {
$data = $game->players()
->withMin(
['statistics' => fn ($query) => $query->whereDate('created_at', '=', now()->subDays($dayNumber))],
'level'
)
->withMax(
['statistics' => fn ($query) => $query->whereDate('created_at', '=', now()->subDays($dayNumber))],
'level'
)
->get();
$filtered = $data->filter(function ($item) {
return $item->statistics_min_level && $item->statistics_max_level;
});
$dayStats= $filtered->values()->map(function ($item) {
$item->levelDifference = $item->statistics_max_level - $item->statistics_min_level;
return $item;
});
$days->put(
$dayNumber,
$dayStats
);
}
return $days;