-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathComment.php
More file actions
148 lines (127 loc) · 3.25 KB
/
Comment.php
File metadata and controls
148 lines (127 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;
use Maize\Markable\Markable;
use Maize\Markable\Models\Bookmark;
use Maize\Markable\Models\Favorite;
use Mpociot\Versionable\VersionableTrait;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
/**
* @property int $id
* @property string $body
* @property int $parent_id
* @property int $post_id
* @property int $user_id
* @property User $user
*/
final class Comment extends BaseModel
{
use HasFactory;
use LogsActivity;
use Markable;
use Searchable;
use SoftDeletes;
use VersionableTrait;
// Properties
protected $fillable = [
'body',
'parent_id',
'post_id',
'user_id',
];
protected static array $marks = [
Bookmark::class,
Favorite::class,
Flag::class,
];
protected array $searchable = [
'text',
];
public function getActivityLogOptions(): LogOptions
{
return LogOptions::defaults()->logFillable();
}
public function toSearchableArray(): array
{
return array_merge($this->toArray(), [
'id' => (string) $this->id,
'body' => $this->body,
'created_at' => $this->created_at->timestamp,
]);
}
// Builders
/*
public function newEloquentBuilder($query): CommentQueryBuilder
{
return new CommentQueryBuilder($query);
}
*/
public function scopeSearch(Builder $query, string $keyword): Builder
{
return $query->whereFullText(
['body'],
"$keyword*",
['mode' => 'boolean'],
);
}
// Relationships
public function bookmarkCount(): int
{
return Bookmark::count($this);
}
public function favoriteCount(): int
{
return Favorite::count($this);
}
public function flagCount(): int
{
return Flag::where([
'markable_id' => $this->getKey(),
'markable_type' => $this->getMorphClass(),
])->count();
}
public function userFlagged(): bool
{
return Flag::where([
'user_id' => auth()->id(),
'markable_id' => $this->getKey(),
'markable_type' => $this->getMorphClass(),
])->exists();
}
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function parent(): BelongsTo
{
return $this->belongsTo(
related: Comment::class,
foreignKey: 'parent_id',
ownerKey: 'id',
);
}
public function replies(): HasMany
{
return $this->hasMany(
related: Comment::class,
foreignKey: 'parent_id',
localKey: 'id',
)->with([
'user',
'bookmarks',
'favorites',
'flags',
]);
}
}