From 5108b1f3eda32f9e61d80d8ffd87f914a1bfa97c Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 11 Jul 2024 16:35:16 +0100 Subject: [PATCH] Capture the log meta for what was changed Signed-off-by: snipe --- app/Observers/ConsumableObserver.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php index 08c56d2ee20a..27b3c7f9761b 100644 --- a/app/Observers/ConsumableObserver.php +++ b/app/Observers/ConsumableObserver.php @@ -16,12 +16,26 @@ class ConsumableObserver */ public function updated(Consumable $consumable) { - $logAction = new Actionlog(); - $logAction->item_type = Consumable::class; - $logAction->item_id = $consumable->id; - $logAction->created_at = date('Y-m-d H:i:s'); - $logAction->user_id = Auth::id(); - $logAction->logaction('update'); + + $changed = []; + + foreach ($consumable->getRawOriginal() as $key => $value) { + // Check and see if the value changed + if ($consumable->getRawOriginal()[$key] != $consumable->getAttributes()[$key]) { + $changed[$key]['old'] = $consumable->getRawOriginal()[$key]; + $changed[$key]['new'] = $consumable->getAttributes()[$key]; + } + } + + if (count($changed) > 0) { + $logAction = new Actionlog(); + $logAction->item_type = Consumable::class; + $logAction->item_id = $consumable->id; + $logAction->created_at = date('Y-m-d H:i:s'); + $logAction->user_id = Auth::id(); + $logAction->log_meta = json_encode($changed); + $logAction->logaction('update'); + } } /**