Post::comments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 1
c 2
b 0
f 2
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\BasePost;
8
use CSlant\LaravelLike\HasLike;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13
/**
14
 * Class Post
15
 *
16
 * @package CSlant\Blog\Core\Models
17
 *
18
 * @property int $id
19
 * @property string $name
20
 * @property string $description
21
 * @property string $content
22
 * @property string $status
23
 * @property int $author_id
24
 * @property string $author_type
25
 * @property int $is_featured
26
 * @property int $views
27
 * @property string $format_type
28
 * @property Carbon $created_at
29
 * @property Carbon $updated_at
30
 * @property Slug $slug
31
 * @property string $url
32
 * @property Tag[] $tags
33
 * @property Category[] $categories
34
 * @property Comment[] $comments
35
 * @property string $image
36
 * @property string $author
37
 *
38
 * @method static Builder|Post newModelQuery()
39
 * @method static Builder|Post newQuery()
40
 * @method static Builder|Post query()
41
 * @method static Builder|Post first()
42
 * @method static Builder|Post find($id)
43
 * @method static Builder|Post with($relations)
44
 * @method static Builder|Post whereId($value)
45
 * @method static Builder|Post whereIn($column, $values)
46
 * @method static Builder|Post where($column, $operator = null, $value = null, $boolean = 'and')
47
 * @method static Post findOrFail($id)
48
 * @method static Post create($data)
49
 *
50
 * @mixin BasePost
51
 */
52
#[AllowDynamicProperties]
53
class Post extends BasePost
54
{
55
    use HasLike;
56
57
    public function slug(): HasOne
58
    {
59
        return $this->hasOne(Slug::class, 'reference_id', 'id')
60
            ->where('reference_type', $this->getBaseModel());
61
    }
62
63
    /**
64
     * The Post has many relationships with the Comment.
65
     *
66
     * @return MorphMany
67
     */
68
    public function comments(): MorphMany
69
    {
70
        return $this->morphMany(Comment::class, 'reference');
71
    }
72
73
    /**
74
     * Get the interaction of the given user.
75
     *
76
     * @param  int  $userId
77
     *
78
     * @return MorphMany
79
     */
80
    public function withInteractionCommentBy(int $userId): MorphMany
81
    {
82
        return $this->comments()->where('author_id', $userId);
83
    }
84
85
    /**
86
     * Check if the model has been interacted by the given user.
87
     *
88
     * @param  int  $userId
89
     *
90
     * @return bool
91
     */
92
    public function isCommentBy(int $userId): bool
93
    {
94
        return $this->withInteractionCommentBy($userId)->exists();
95
    }
96
}
97