schedule); $maintenancePlanned = $maintenance->history()->create([ 'start_at' => Carbon::createFromTimestamp($cron->getNext()), ]); $maintenancePlanned->refresh(); $hosts = $maintenance->hosts; foreach ($hosts as $key => $host) { $maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([ 'host_id' => $host->id ]); $tasks = $maintenance->tasks; foreach ($tasks as $key => $task) { $maintenancePlannedHost->historyTasks()->create([ 'maintenance_task_id' => $task->id, 'maintenance_history_id' => $maintenancePlanned->id ]); } } } } }