where('updated_at', '>', now()->subHours(2)) ->cursor() ->each(function ($project) { $project->current_hours = $this->calculateDuration($project); $project->save(); }); } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); Project::with('tasks')->where('updated_at', '>', now()->subHours(2)) ->cursor() ->each(function ($project) { $project->current_hours = $this->calculateDuration($project); $project->save(); }); } } } private function calculateDuration($project): int { $duration = 0; $project->tasks->each(function ($task) use (&$duration) { foreach(json_decode($task->time_log) as $log){ $start_time = $log[0]; $end_time = $log[1] == 0 ? time() : $log[1]; $duration += $end_time - $start_time; } return round(($duration/60/60), 0); }); } }