false]); // Get all companies $companies = Company::enabled()->withCount('reports')->cursor(); foreach ($companies as $company) { // Has company reports if (!$company->reports_count) { continue; } $this->info('Calculating reports for ' . $company->name . ' company.'); // Set company $company->makeCurrent(); $this->cacheReportsOfCurrentCompany(); } Company::forgetCurrent(); } protected function cacheReportsOfCurrentCompany() { $reports = Report::orderBy('name')->get(); foreach ($reports as $report) { $class = Utility::getClassInstance($report, false); if (empty($class)) { continue; } $ttl = 3600 * 6; // 6 hours Cache::forget('reports.totals.' . $report->id); Cache::remember('reports.totals.' . $report->id, $ttl, function () use ($class) { return $class->getGrandTotal(); }); } } }