Completed
Push — master ( fec5e4...9d55fa )
by Vojta
01:20
created

Category::getBrandsCountAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace VojtaSvoboda\Brands\Models;
2
3
use Model;
4
use October\Rain\Database\Traits\SoftDelete as SoftDeletingTrait;
5
use October\Rain\Database\Traits\Sortable as SortableTrait;
6
use October\Rain\Database\Traits\Validation as ValidationTrait;
7
8
class Category extends Model
9
{
10
    use SoftDeletingTrait;
11
12
    use SortableTrait;
13
14
    use ValidationTrait;
15
16
    public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
17
18
    public $table = 'vojtasvoboda_brands_categories';
19
20
    public $rules = [
21
        'name' => 'required|max:255',
22
        'slug' => 'required|unique:vojtasvoboda_brands_categories',
23
        'enabled' => 'boolean',
24
        'description' => 'max:10000',
25
    ];
26
27
    public $translatable = ['name', 'slug', 'description'];
28
29
    public $dates = ['created_at', 'updated_at', 'deleted_at'];
30
31
    public $attachOne = [
32
        'image' => ['System\Models\File'],
33
    ];
34
35
    public $belongsToMany = [
36
        'brands' => [
37
            'VojtaSvoboda\Brands\Models\Brand',
38
            'table' => 'vojtasvoboda_brands_brand_category',
39
            'order' => 'name desc',
40
            'scope' => 'isEnabled',
41
            'timestamps' => true,
42
        ],
43
    ];
44
45
    public function scopeIsEnabled($query)
46
    {
47
        return $query->where('enabled', true);
48
    }
49
50
    public function getBrandsCountAttribute()
51
    {
52
        return $this->brands()->count();
53
    }
54
}
55