maintenanceHistory = MaintenanceHistory::with(['historyHosts', 'historyHosts.historyTasks', 'historyHosts.historyTasks.maintenanceTask', 'historyHosts.historyTasks.maintenanceTask.task'])->find($maintenanceHistory); foreach ($this->maintenanceHistory->historyHosts as $maintenanceHost) { foreach ($maintenanceHost->historyTasks as $maintenanceTask) { $this->maintenance_task_status[$maintenanceHost->id][$maintenanceTask->id] = (!empty($maintenanceTask->finished_at) ? true : false); } } } public function updatedMaintenanceTaskStatus($value) { $hosts = $this->maintenanceHistory->historyHosts()->whereIn('maintenance_host_histories.id', array_keys($this->maintenance_task_status))->get()->keyBy(['id']); foreach ($this->maintenance_task_status as $host_history_id => $tasks) { $done = []; $notDone = []; foreach ($tasks as $task_history_id => $state) { if ($state === true) { $done[] = $task_history_id; continue; } $notDone[] = $task_history_id; } if (count($done) != 0) { $hosts[$host_history_id]->historyTasks() ->whereIn('maintenance_task_histories.id', $done) ->update([ 'maintenance_task_histories.maintenance_host_history_id' => $host_history_id, 'maintenance_task_histories.status' => 2, 'maintenance_task_histories.finished_at' => Carbon::now(), ]); } if (count($notDone) != 0) { $this->maintenanceHistory->historyHosts() ->find($host_history_id)->historyTasks() ->whereIn('maintenance_task_histories.id', $notDone) ->update([ 'maintenance_task_histories.maintenance_host_history_id' => $host_history_id, 'maintenance_task_histories.status' => 1, 'maintenance_task_histories.finished_at' => null, ]); } } } public function render() { return view('livewire.maintenance.progress-form'); } }