Use the observer to delete associated files and detach users

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-11 17:00:38 +01:00
parent d4a66f9b6c
commit c2fe3b5459
2 changed files with 25 additions and 1 deletions

View file

@ -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'));

View file

@ -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;