startOfMonth(); // end of this month $endOfMonth = now()->endOfMonth(); Module::chunk(100, function ($modules) use ($beginOfMonth, $endOfMonth) { foreach ($modules as $module) { $this_month = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); // this month transactions $this_month = [ 'balance' => $this_month->sum('outcome'), 'drops' => $this_month->sum('outcome_drops') ]; Cache::put('this_month_balance_and_drops_' . $module->id, $this_month, 60 * 24 * 30); // last month transactions $last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); $last_moth = [ 'balance' => $last_moth->sum('outcome'), 'drops' => $last_moth->sum('outcome_drops') ]; Cache::put('last_month_balance_and_drops_' . $module->id, $last_moth, 60 * 24 * 30); } }); return 0; } }