file_number)) { $max = DB::table('patients')->max('file_number'); $patient->file_number = max(100, ($max ?? 99) + 1); } } public function updating(Patient $patient): void { foreach (self::FILE_FIELDS as $field) { $rawOld = $patient->getRawOriginal($field); $oldFiles = ! empty($rawOld) ? (json_decode($rawOld, true) ?? []) : []; $newFiles = array_values((array) ($patient->getAttribute($field) ?? [])); $removed = array_diff($oldFiles, $newFiles); foreach ($removed as $path) { if (! empty(trim((string) $path))) { Storage::disk('public')->delete($path); } } } } public function deleting(Patient $patient): void { foreach (self::FILE_FIELDS as $field) { $files = $patient->getAttribute($field) ?? []; foreach ((array) $files as $path) { Storage::disk('public')->delete($path); } } } }