schedule); $nextRunTime = Carbon::createFromTimestamp($cron->getNext()); if(MaintenanceHistory::where('hash', md5($maintenance->id . $nextRunTime))->first() === null){ continue; }; $maintenancePlanned = $maintenance->history()->create([ 'start_at' => $nextRunTime, 'guestor_id' => $maintenance->guestor_id, ]); $maintenancePlanned->refresh(); $hosts = $maintenance->hosts; foreach ($hosts as $key => $host) { $maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([ 'host_id' => $host->id ]); $tasks = $maintenance->hosts->find($host->id)->tasks; foreach ($tasks as $key => $task) { $maintenancePlannedHost->historyTasks()->create([ 'maintenance_task_id' => $task->id, 'maintenance_history_id' => $maintenancePlanned->id ]); } } } } }