From 1bd739253187b999501b3dca56e3083e6abf07e8 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 26 Dec 2017 17:32:45 -0800 Subject: [PATCH] Account for audits on deleted assets --- .../2017_12_26_170856_re_normalize_last_audit.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/database/migrations/2017_12_26_170856_re_normalize_last_audit.php b/database/migrations/2017_12_26_170856_re_normalize_last_audit.php index efde462fc8..d8a33492fb 100644 --- a/database/migrations/2017_12_26_170856_re_normalize_last_audit.php +++ b/database/migrations/2017_12_26_170856_re_normalize_last_audit.php @@ -17,6 +17,7 @@ class ReNormalizeLastAudit extends Migration { if (Schema::hasColumn('assets', 'last_audit_date')) { + // Grab the latest info from the Actionlog table where the action is 'audit' $audits = Actionlog::selectRaw('MAX(created_at) AS created_at, item_id') ->where('action_type', 'audit') @@ -27,10 +28,13 @@ class ReNormalizeLastAudit extends Migration if ($audits) { foreach ($audits as $audit) { - $assets = Asset::where('id', $audit->item_id)->first(); - $assets->last_audit_date = $audit->created_at; - $assets->unsetEventDispatcher(); - $assets->save(); + $asset = Asset::where('id', $audit->item_id)->withTrashed()->first(); + if ($asset) { + $asset->last_audit_date = $audit->created_at; + $asset->unsetEventDispatcher(); + $asset->save(); + } + } } }