mirror of
https://github.com/snipe/snipe-it.git
synced 2026-08-18 11:15:42 +00:00
Use relation instead of property access to prevent full hydration
This commit is contained in:
@ -213,17 +213,23 @@ class Category extends SnipeModel
|
||||
return $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;
|
||||
}
|
||||
|
||||
@ -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')
|
||||
|
||||
Reference in New Issue
Block a user