1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMessenger\Models; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMessenger\Contracts\Message as MessageContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Message |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\LaravelMessenger\Models |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property int id |
16
|
|
|
* @property int discussion_id |
17
|
|
|
* @property string participable_type |
18
|
|
|
* @property int participable_id |
19
|
|
|
* @property int body |
20
|
|
|
* @property \Illuminate\Support\Carbon created_at |
21
|
|
|
* @property \Illuminate\Support\Carbon updated_at |
22
|
|
|
* @property \Illuminate\Support\Carbon deleted_at |
23
|
|
|
* |
24
|
|
|
* @property \Arcanedev\LaravelMessenger\Models\Discussion discussion |
25
|
|
|
* @property \Illuminate\Database\Eloquent\Model participable |
26
|
|
|
* @property \Illuminate\Database\Eloquent\Model author |
27
|
|
|
* @property \Illuminate\Database\Eloquent\Collection participations |
28
|
|
|
* @property \Illuminate\Database\Eloquent\Collection recipients |
29
|
|
|
*/ |
30
|
|
|
class Message extends Model implements MessageContract |
31
|
|
|
{ |
32
|
|
|
/* ----------------------------------------------------------------- |
33
|
|
|
| Properties |
34
|
|
|
| ----------------------------------------------------------------- |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The relationships that should be touched on save. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $touches = ['discussion']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The attributes that can be set with Mass Assignment. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $fillable = [ |
50
|
|
|
'discussion_id', |
51
|
|
|
'body', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The attributes that should be mutated to dates. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $dates = ['deleted_at']; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The attributes that should be cast to native types. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $casts = [ |
67
|
|
|
'id' => 'integer', |
68
|
|
|
'discussion_id' => 'integer', |
69
|
|
|
'participable_id' => 'integer', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/* ----------------------------------------------------------------- |
73
|
|
|
| Constructor |
74
|
|
|
| ----------------------------------------------------------------- |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create a new Eloquent model instance. |
79
|
|
|
* |
80
|
|
|
* @param array $attributes |
81
|
|
|
*/ |
82
|
72 |
|
public function __construct(array $attributes = []) |
83
|
|
|
{ |
84
|
72 |
|
$this->setTable( |
85
|
72 |
|
config('messenger.messages.table', 'messages') |
86
|
|
|
); |
87
|
|
|
|
88
|
72 |
|
parent::__construct($attributes); |
89
|
72 |
|
} |
90
|
|
|
|
91
|
|
|
/* ----------------------------------------------------------------- |
92
|
|
|
| Relationships |
93
|
|
|
| ----------------------------------------------------------------- |
94
|
|
|
*/ |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Discussion relationship. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
100
|
|
|
*/ |
101
|
66 |
|
public function discussion() |
102
|
|
|
{ |
103
|
66 |
|
return $this->belongsTo( |
104
|
66 |
|
config('messenger.discussions.model', Discussion::class) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* User/Author relationship (alias). |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
112
|
|
|
*/ |
113
|
12 |
|
public function author() |
114
|
|
|
{ |
115
|
12 |
|
return $this->participable(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Participable relationship. |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
122
|
|
|
*/ |
123
|
12 |
|
public function participable() |
124
|
|
|
{ |
125
|
12 |
|
return $this->morphTo(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Participations relationship. |
130
|
|
|
* |
131
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
132
|
|
|
*/ |
133
|
6 |
|
public function participations() |
134
|
|
|
{ |
135
|
6 |
|
return $this->hasMany( |
136
|
6 |
|
config('messenger.participations.model', Participation::class), |
137
|
6 |
|
'discussion_id', |
138
|
6 |
|
'discussion_id' |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/* ----------------------------------------------------------------- |
143
|
|
|
| Getters & Setters |
144
|
|
|
| ----------------------------------------------------------------- |
145
|
|
|
*/ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Recipients of this message. |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
151
|
|
|
*/ |
152
|
6 |
|
public function getRecipientsAttribute() |
153
|
|
|
{ |
154
|
6 |
|
$morph = config('messenger.users.morph', 'participable'); |
155
|
|
|
|
156
|
|
|
return $this->participations->reject(function (Participation $participant) use ($morph) { |
157
|
6 |
|
return $participant->getAttribute("{$morph}_id") === $this->getAttribute("{$morph}_id") |
158
|
6 |
|
&& $participant->getAttribute("{$morph}_type") === $this->getAttribute("{$morph}_type"); |
159
|
6 |
|
}); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|