A free open source IT asset/license management system
Use relation instead of property access to prevent full hydration
snipe98513508bb81
diff --git a/app/Models/Category.php b/app/Models/Category.phpindex 6244353924..230b4bddb3 100755--- a/app/Models/Category.php+++ b/app/Models/Category.php@@ -213,17 +213,23 @@ class Category extends SnipeModelreturn $this->{Str::plural($this->category_type).'_count'};}+ // Use the relation method (->assets()) instead of property access+ // (->assets) — the property form hydrates the entire collection+ // just to read its size. On an asset category with thousands of+ // rows that's thousands of model instances allocated for a single+ // count comparison (visible as huge "Retrieved Models" totals in+ // Debugbar). Method-style emits a single SELECT count(*).switch ($this->category_type) {case 'asset':- return $this->assets->count();+ return $this->assets()->count();case 'accessory':- return $this->accessories->count();+ return $this->accessories()->count();case 'component':- return $this->components->count();+ return $this->components()->count();case 'consumable':- return $this->consumables->count();+ return $this->consumables()->count();case 'license':- return $this->licenses->count();+ return $this->licenses()->count();default:return 0;}diff --git a/resources/views/categories/view.blade.php b/resources/views/categories/view.blade.phpindex 5e0c03d1c1..49624d003b 100644--- a/resources/views/categories/view.blade.php+++ b/resources/views/categories/view.blade.php@@ -18,18 +18,16 @@<x-page-column class="col-md-9 main-panel"><x-tabs><x-slot:tabnav>+ {{-- Method-style ->blah()->count() so we issue a+ SELECT count(*) instead of hydrating the full+ collection just to read its size. --}}+@if ($category->category_type=='asset')<x-tabs.asset-tab count="{{ $category->showableAssets()->count() }}"/>- {{-- Use the relation method (->models()) — property access (->models) hydrates- the whole AssetModel collection just to take ->count(), and on a category- with hundreds of models it's a notable allocator on the shell render. --}}<x-tabs.model-tab count="{{ $category->models()->count() }}"/>@elseif ($category->category_type=='accessory')- {{-- Method-style ->accessories()->count() so we issue a- SELECT count(*) instead of hydrating the full- collection just to read its size. Same rationale- as the ->models() call above. --}}- <x-tabs.accessory-tab count="{{ $category->accessories()->count() }}"/>++ <x-tabs.accessory-tab count="{{ $category->accessories()->count() }}"/>@elseif ($category->category_type=='license')<x-tabs.license-tab count="{{ $category->licenses()->count() }}"/>@elseif ($category->category_type=='consumable')