mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-12 06:17:28 -08:00
Added notes to itemCount()
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
249b188654
commit
bef4224e14
|
@ -98,11 +98,8 @@ class Category extends SnipeModel
|
|||
*/
|
||||
public function isDeletable()
|
||||
{
|
||||
if (Gate::allows('delete', $this)) {
|
||||
$category_type_var = Str::plural($this->category_type).'_count';
|
||||
return $this->{$category_type_var};
|
||||
}
|
||||
|
||||
return Gate::allows('delete', $this)
|
||||
&& ($this->{Str::plural($this->category_type).'_count'} == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,7 +149,34 @@ class Category extends SnipeModel
|
|||
{
|
||||
return $this->hasMany(\App\Models\Component::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the number of items in the category. This should NEVER be used in
|
||||
* a collection of cartegories, as you'll end up with an n+1 query problem.
|
||||
*
|
||||
* It should only be used in a single categoiry context.
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
public function itemCount()
|
||||
{
|
||||
switch ($this->category_type) {
|
||||
case 'asset':
|
||||
return $this->assets()->count();
|
||||
case 'accessory':
|
||||
return $this->accessories()->count();
|
||||
case 'component':
|
||||
return $this->components()->count();
|
||||
case 'consumable':
|
||||
return $this->consumables()->count();
|
||||
case 'license':
|
||||
return $this->licenses()->count();
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Establishes the category -> assets relationship
|
||||
|
|
Loading…
Reference in a new issue