User::getPostsCountAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace CSlant\Blog\Core\Models;
4
5
use AllowDynamicProperties;
6
use Carbon\Carbon;
7
use CSlant\Blog\Core\Models\Base\BaseUser;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
11
/**
12
 * Class User
13
 *
14
 * @package CSlant\Blog\Core\Models
15
 *
16
 * @property int $id
17
 * @property string $name
18
 * @property string $email
19
 * @property string $password
20
 * @property string $remember_token
21
 * @property string $first_name
22
 * @property string $last_name
23
 * @property string $username
24
 * @property object $permissions
25
 * @property int $super_user
26
 * @property Carbon $created_at
27
 * @property string $avatar_url Property for avatar image url
28
 * @property int $posts_count
29
 * @property null|Role $roles
30
 * @property Post[] $posts
31
 *
32
 * @method static Builder|User newModelQuery()
33
 * @method static Builder|User newQuery()
34
 * @method static Builder|User query()
35
 * @method static Builder|User first()
36
 * @method static Builder|User find($id)
37
 * @method static Builder|User whereCreatedAt($value)
38
 *
39
 */
40
#[AllowDynamicProperties]
41
class User extends BaseUser
42
{
43
    /**
44
     * Defining an posts count accessor
45
     * ->posts_count
46
     *
47
     * @return int
48
     */
49
    public function getPostsCountAttribute(): int
50
    {
51
        return $this->posts()->wherePublished()->count();
52
    }
53
54
    public function posts(): HasMany
55
    {
56
        return $this->hasMany(Post::class, 'author_id')->wherePublished();
57
    }
58
}
59