1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Eloquence\Behaviours\CountCache\Countable; |
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Xetaio\Mentions\Models\Traits\HasMentionsTrait; |
7
|
|
|
use Xetaravel\Models\Gates\FloodGate; |
8
|
|
|
use Xetaravel\Models\Presenters\DiscussPostPresenter; |
9
|
|
|
|
10
|
|
|
class DiscussPost extends Model |
11
|
|
|
{ |
12
|
|
|
use Countable, |
13
|
|
|
DiscussPostPresenter, |
14
|
|
|
FloodGate, |
15
|
|
|
HasMentionsTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The attributes that are mass assignable. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $fillable = [ |
23
|
|
|
'user_id', |
24
|
|
|
'conversation_id', |
25
|
|
|
'content', |
26
|
|
|
'is_edited' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The accessors to append to the model's array form. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $appends = [ |
35
|
|
|
'content_markdown', |
36
|
|
|
'post_url' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The attributes that should be mutated to dates. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $dates = [ |
45
|
|
|
'edited_at' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The "booting" method of the model. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected static function boot() |
54
|
|
|
{ |
55
|
|
|
parent::boot(); |
56
|
|
|
|
57
|
|
|
// Set the user id to the new post before saving it. |
58
|
|
|
static::creating(function ($model) { |
59
|
|
|
$model->user_id = Auth::id(); |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the count cache configuration. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function countCaches(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'discuss_post_count' => [User::class, 'user_id', 'id'], |
72
|
|
|
'post_count' => [DiscussConversation::class, 'conversation_id', 'id'] |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the user that owns the post. |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
80
|
|
|
*/ |
81
|
|
|
public function user() |
82
|
|
|
{ |
83
|
|
|
return $this->belongsTo(User::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the conversation that owns the post. |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
90
|
|
|
*/ |
91
|
|
|
public function conversation() |
92
|
|
|
{ |
93
|
|
|
return $this->belongsTo(DiscussConversation::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the user that edited the post. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
100
|
|
|
*/ |
101
|
|
|
public function editedUser() |
102
|
|
|
{ |
103
|
|
|
return $this->hasOne(User::class, 'id', 'edited_user_id'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|