diff --git a/app/Livewire/Settings.php b/app/Livewire/Settings.php index d594526..7de9a26 100644 --- a/app/Livewire/Settings.php +++ b/app/Livewire/Settings.php @@ -102,14 +102,13 @@ class Settings extends Component 'logo' => 'nullable|image|mimes:png,webp|max:2048', ]); + // Store the new upload, but keep the old file until the DB row that + // references it is updated — delete the old one only after that commits, + // so a failed update never orphans a file or dangles a reference. + $oldToDelete = null; if ($this->logo !== null) { - // Store the new file FIRST; only drop the old one once the write - // succeeded, so a failed upload never leaves a dangling reference. - $old = $this->brandLogoPath; + $oldToDelete = $this->brandLogoPath; $this->brandLogoPath = $this->logo->store('branding', 'public'); - if ($old !== null && $old !== $this->brandLogoPath) { - Storage::disk('public')->delete($old); - } $this->logo = null; } @@ -120,6 +119,10 @@ class Settings extends Component 'brand_logo_path' => $this->brandLogoPath, ]); + if ($oldToDelete !== null && $oldToDelete !== $this->brandLogoPath) { + Storage::disk('public')->delete($oldToDelete); + } + $this->dispatch('notify', message: __('settings.branding_saved')); }