schedule); if (!$valid) { dd($maintenance->schedule); return; } $cron = new CronExpression($maintenance->schedule); $maintenancePlanned = $maintenance->history()->create([ 'start_at' => $cron->getNextRunDate(null, 2) ]); $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 ]); } } } die(); } }