The issue might be with the way the $month variable is being used inside the Cache::remember closure. Since it's a variable inside a closure, it needs to be passed in using the use keyword. Here's an updated version of the code that should work:
public function getListYearGame($year, $month)
{
if ($month < "13") {
$expire = Carbon::now()->addMinutes(10);
$name = 'listGame' . $month . '' . $year;
$el_giochi = Cache::remember($name, $expire, function () use ($month, $year) {
return InfoGame::with(
'games:id,nome,copertina,cover,slug',
'platform',
'review',
'infogame.platform'
)
->whereHas('Games', function (Builder $query) {
$query->where('active', 1);
})
->whereMonth('data', '=', $month)
->whereYear('data', $year)
->groupBy('id_game')
->orderBy('data')
->get();
});
}
}
By adding use ($month, $year) after the function definition, we're telling PHP to include those variables inside the closure.