#!/usr/bin/env python3 path = '/home/forge/app.charityright.org.uk/vendor/filament/tables/src/Table/Concerns/HasRecords.php' with open(path, 'r') as f: lines = f.readlines() # Find the getModel method and replace it new_lines = [] i = 0 while i < len(lines): line = lines[i] if 'public function getModel(): string' in line and 'getModelLabel' not in line: # Replace the entire method (next lines until closing brace) new_lines.append(' public function getModel(): string\n') new_lines.append(' {\n') new_lines.append(' $query = $this->getQuery();\n') new_lines.append(' $model = $query->getModel();\n') new_lines.append(' if ($model === null) {\n') new_lines.append(' $lw = get_class($this->getLivewire());\n') new_lines.append(" \\Illuminate\\Support\\Facades\\Log::error('Filament getModel null for: ' . $lw);\n") new_lines.append(" throw new \\TypeError('getModel null for: ' . $lw);\n") new_lines.append(' }\n') new_lines.append(' return $model::class;\n') new_lines.append(' }\n') # Skip the old method body i += 1 while i < len(lines) and lines[i].strip() != '}': i += 1 i += 1 # skip closing brace continue new_lines.append(line) i += 1 with open(path, 'w') as f: f.writelines(new_lines) print('Patched successfully')