1
|
|
|
<?php namespace Arcanedev\LaravelMessenger\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMessenger\Bases\Model; |
4
|
|
|
use Arcanedev\LaravelMessenger\Contracts\Message as MessageContract; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Message |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelMessenger\Models |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @property int id |
13
|
|
|
* @property int discussion_id |
14
|
|
|
* @property \Arcanedev\LaravelMessenger\Models\Discussion discussion |
15
|
|
|
* @property int user_id |
16
|
|
|
* @property \Illuminate\Database\Eloquent\Model user |
17
|
|
|
* @property \Illuminate\Database\Eloquent\Model author |
18
|
|
|
* @property int body |
19
|
|
|
* @property \Carbon\Carbon created_at |
20
|
|
|
* @property \Carbon\Carbon updated_at |
21
|
|
|
* @property \Illuminate\Database\Eloquent\Collection participants |
22
|
|
|
* @property \Illuminate\Database\Eloquent\Collection recipients |
23
|
|
|
*/ |
24
|
|
|
class Message extends Model implements MessageContract |
25
|
|
|
{ |
26
|
|
|
/* ------------------------------------------------------------------------------------------------ |
27
|
|
|
| Properties |
28
|
|
|
| ------------------------------------------------------------------------------------------------ |
29
|
|
|
*/ |
30
|
|
|
/** |
31
|
|
|
* The relationships that should be touched on save. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $touches = ['discussion']; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The attributes that can be set with Mass Assignment. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fillable = ['discussion_id', 'user_id', 'body']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Validation rules. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $rules = [ |
50
|
|
|
'body' => 'required', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/* ------------------------------------------------------------------------------------------------ |
54
|
|
|
| Constructor |
55
|
|
|
| ------------------------------------------------------------------------------------------------ |
56
|
|
|
*/ |
57
|
|
|
/** |
58
|
|
|
* Create a new Eloquent model instance. |
59
|
|
|
* |
60
|
|
|
* @param array $attributes |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $attributes = []) |
63
|
|
|
{ |
64
|
|
|
$this->setTable( |
65
|
|
|
$this->getTableFromConfig('messages', 'messages') |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
parent::__construct($attributes); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/* ------------------------------------------------------------------------------------------------ |
72
|
|
|
| Relationships |
73
|
|
|
| ------------------------------------------------------------------------------------------------ |
74
|
|
|
*/ |
75
|
|
|
/** |
76
|
|
|
* Discussion relationship. |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
79
|
|
|
*/ |
80
|
|
|
public function discussion() |
81
|
|
|
{ |
82
|
|
|
return $this->belongsTo( |
83
|
|
|
$this->getModelFromConfig('discussions', Discussion::class) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* User/Author relationship (alias). |
89
|
|
|
* |
90
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
91
|
|
|
*/ |
92
|
|
|
public function author() |
93
|
|
|
{ |
94
|
|
|
return $this->user(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* User/Author relationship. |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
101
|
|
|
*/ |
102
|
|
|
public function user() |
103
|
|
|
{ |
104
|
|
|
return $this->belongsTo( |
105
|
|
|
$this->getModelFromConfig('users') |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Participants relationship. |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
113
|
|
|
*/ |
114
|
|
|
public function participants() |
115
|
|
|
{ |
116
|
|
|
return $this->hasMany( |
117
|
|
|
$this->getModelFromConfig('participants', Participant::class), |
118
|
|
|
'discussion_id', |
119
|
|
|
'discussion_id' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* ------------------------------------------------------------------------------------------------ |
124
|
|
|
| Getters & Setters |
125
|
|
|
| ------------------------------------------------------------------------------------------------ |
126
|
|
|
*/ |
127
|
|
|
/** |
128
|
|
|
* Recipients of this message. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
131
|
|
|
*/ |
132
|
|
|
public function getRecipientsAttribute() |
133
|
|
|
{ |
134
|
|
|
return $this->participants->filter(function (Participant $participant) { |
135
|
|
|
return $participant->user_id != $this->user_id; |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|