Skip to content

A free open source IT asset/license management system

Added forHistory scope in ActionLog, use it in Loggable

snipe9b43835e2d7a
diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php
index f704d3f42c..18a01ece2a 100755
--- a/app/Models/Actionlog.php
+++ b/app/Models/Actionlog.php
@@ -9,6 +9,7 @@ use App\Presenters\ActionlogPresenter;
use App\Presenters\Presentable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
@@ -328,6 +329,27 @@ class Actionlog extends SnipeModel
return $this->morphTo('target')->withTrashed();
}
+ /**
+ * Eager load history relations used by the API transformer to avoid N+1 queries.
+ */
+ public function scopeForApiHistory($query)
+ {
+ return $query->with([
+ 'adminuser',
+ 'location',
+ 'item' => function (MorphTo $morphTo) {
+ $morphTo->morphWith([
+ Asset::class => ['model'],
+ ]);
+ },
+ 'target' => function (MorphTo $morphTo) {
+ $morphTo->morphWith([
+ Asset::class => ['model'],
+ ]);
+ },
+ ]);
+ }
+
/**
* Establishes the actionlog -> location relationship
*
diff --git a/app/Models/Traits/Loggable.php b/app/Models/Traits/Loggable.php
index a5cf9d9049..ef816cb76a 100644
--- a/app/Models/Traits/Loggable.php
+++ b/app/Models/Traits/Loggable.php
@@ -107,7 +107,7 @@ trait Loggable
break;
}
- return $history;
+ return $history->forApiHistory();
}