record->getPathRelativeToRoot()) { $this->uploadedFile = $file; // Keep the original file_name to prevent breaking existing references // $data['file_name'] is not updated - we preserve the original filename $data['mime_type'] = Storage::disk('public')->exists($file) ? Storage::disk('public')->mimeType($file) : 'application/octet-stream'; $data['size'] = Storage::disk('public')->exists($file) ? Storage::disk('public')->size($file) : 0; } return $data; } protected function afterSave(): void { if ($this->uploadedFile && $this->record) { $disk = Storage::disk('public'); $mediaDirectory = (string) $this->record->id; // Delete old file if it exists $oldPath = $mediaDirectory.'/'.$this->record->getOriginal('file_name'); if ($disk->exists($oldPath)) { $disk->delete($oldPath); } // Move new file to Spatie's expected location using the original filename if ($disk->exists($this->uploadedFile)) { $disk->makeDirectory($mediaDirectory); // Use the original file_name to preserve existing references $newPath = $mediaDirectory.'/'.$this->record->file_name; $disk->move($this->uploadedFile, $newPath); } // Redirect to the same page to refresh the form state $this->redirect(static::getUrl(['record' => $this->record]), navigate: true); } } }