diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php index f6b4f0139b..5685944a5c 100644 --- a/app/Http/Controllers/Consumables/ConsumablesController.php +++ b/app/Http/Controllers/Consumables/ConsumablesController.php @@ -182,7 +182,7 @@ class ConsumablesController extends Controller return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found')); } $this->authorize($consumable); - $consumable->users()->detach(); + $consumable->delete(); // Redirect to the locations management page return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.delete.success')); diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php index 27b3c7f976..1bd6e7cde8 100644 --- a/app/Observers/ConsumableObserver.php +++ b/app/Observers/ConsumableObserver.php @@ -5,6 +5,8 @@ namespace App\Observers; use App\Models\Actionlog; use App\Models\Consumable; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; class ConsumableObserver { @@ -66,6 +68,28 @@ class ConsumableObserver */ public function deleting(Consumable $consumable) { + + $consumable->users()->detach(); + + foreach ($consumable->uploads() as $file) { + try { + Storage::disk('public')->delete('consumables/'.$file); + } catch (\Exception $e) { + Log::info($e); + } + } + + try { + Storage::disk('public')->delete('consumables/'.$consumable->image); + } catch (\Exception $e) { + Log::info($e); + } + + $consumable->image = null; + $consumable->save(); + + + $logAction = new Actionlog(); $logAction->item_type = Consumable::class; $logAction->item_id = $consumable->id;