Category::getPostsCountAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CSlant\Blog\Core\Models;
4
5
use AllowDynamicProperties;
6
use Carbon\Carbon;
7
use CSlant\Blog\Core\Models\Base\BaseCategory;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
11
/**
12
 * Class Category
13
 *
14
 * @package CSlant\Blog\Core\Models
15
 *
16
 * @property int $id
17
 * @property string $name
18
 * @property int $parent_id
19
 * @property string $description
20
 * @property string $status
21
 * @property int $author_id
22
 * @property string $author_type
23
 * @property string $icon
24
 * @property int $order
25
 * @property int $is_featured
26
 * @property int $is_default
27
 * @property int $posts_count
28
 * @property Carbon $created_at
29
 * @property Carbon $updated_at
30
 * @property Slug $slug
31
 * @property string $url
32
 * @property Category $parent
33
 * @property Category[] $children
34
 *
35
 * @method static Builder|Category newModelQuery()
36
 * @method static Builder|Category newQuery()
37
 * @method static Builder|Category query()
38
 * @method static Builder|Category first()
39
 * @method static Builder|Category find($id)
40
 * @method static Builder|Category with($relations)
41
 * @method static Builder|Category whereId($value)
42
 * @method static Builder|Category whereIn($column, $values)
43
 * @method static Builder|Category where($column, $operator = null, $value = null, $boolean = 'and')
44
 * @method static Builder|Category findOrFail($id)
45
 * @method static Builder|Category create($data)
46
 * @method static Builder|Category wherePublished()
47
 *
48
 * @mixin BaseCategory
49
 */
50
#[AllowDynamicProperties]
51
class Category extends BaseCategory
52
{
53
    public function slug(): HasOne
54
    {
55
        return $this->hasOne(Slug::class, 'reference_id', 'id')
56
            ->where('reference_type', $this->getBaseModel());
57
    }
58
59
    /**
60
     * Defining an posts count accessor
61
     * ->posts_count
62
     *
63
     * @return int
64
     */
65
    public function getPostsCountAttribute(): int
66
    {
67
        return $this->posts()->wherePublished()->count();
68
    }
69
}
70