1 | <?php namespace Arcanedev\LaravelMessenger\Models; |
||
35 | class Discussion extends Model implements DiscussionContract |
||
36 | { |
||
37 | /* ----------------------------------------------------------------- |
||
38 | | Traits |
||
39 | | ----------------------------------------------------------------- |
||
40 | */ |
||
41 | use SoftDeletes; |
||
42 | |||
43 | /* ----------------------------------------------------------------- |
||
44 | | Properties |
||
45 | | ----------------------------------------------------------------- |
||
46 | */ |
||
47 | /** |
||
48 | * The attributes that can be set with Mass Assignment. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $fillable = ['subject']; |
||
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 | ]; |
||
69 | |||
70 | /* ----------------------------------------------------------------- |
||
71 | | Constructor |
||
72 | | ----------------------------------------------------------------- |
||
73 | */ |
||
74 | /** |
||
75 | * Create a new Eloquent model instance. |
||
76 | * |
||
77 | * @param array $attributes |
||
78 | */ |
||
79 | 354 | public function __construct(array $attributes = []) |
|
87 | |||
88 | /* ----------------------------------------------------------------- |
||
89 | | Relationships |
||
90 | | ----------------------------------------------------------------- |
||
91 | */ |
||
92 | /** |
||
93 | * Participants relationship. |
||
94 | * |
||
95 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
96 | */ |
||
97 | 252 | public function participants() |
|
103 | |||
104 | /** |
||
105 | * Messages relationship. |
||
106 | * |
||
107 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
108 | */ |
||
109 | 99 | public function messages() |
|
115 | |||
116 | /** |
||
117 | * User's relationship. |
||
118 | * |
||
119 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
120 | */ |
||
121 | 9 | public function users() |
|
130 | |||
131 | /** |
||
132 | * Get the user that created the first message. |
||
133 | * |
||
134 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
135 | */ |
||
136 | 9 | public function creator() |
|
140 | |||
141 | /* ----------------------------------------------------------------- |
||
142 | | Scopes |
||
143 | | ----------------------------------------------------------------- |
||
144 | */ |
||
145 | /** |
||
146 | * Scope discussions that the user is associated with. |
||
147 | * |
||
148 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
149 | * @param int $userId |
||
150 | * |
||
151 | * @return \Illuminate\Database\Eloquent\Builder |
||
152 | */ |
||
153 | 18 | public function scopeForUser(Builder $query, $userId) |
|
164 | |||
165 | /** |
||
166 | * Scope discussions with new messages that the user is associated with. |
||
167 | * |
||
168 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
169 | * @param int $userId |
||
170 | * |
||
171 | * @return \Illuminate\Database\Eloquent\Builder |
||
172 | */ |
||
173 | 9 | public function scopeForUserWithNewMessages(Builder $query, $userId) |
|
187 | |||
188 | /** |
||
189 | * Scope discussions between given user ids. |
||
190 | * |
||
191 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
192 | * @param array $userIds |
||
193 | * |
||
194 | * @return \Illuminate\Database\Eloquent\Builder |
||
195 | */ |
||
196 | 9 | public function scopeBetween(Builder $query, array $userIds) |
|
206 | |||
207 | /** |
||
208 | * Get the participants table name. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 27 | protected function getParticipantsTable() |
|
216 | |||
217 | /** |
||
218 | * Scope the query by the subject. |
||
219 | * |
||
220 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
221 | * @param string $subject |
||
222 | * @param bool $strict |
||
223 | * |
||
224 | * @return \Illuminate\Database\Eloquent\Builder |
||
225 | */ |
||
226 | 18 | public function scopeSubject(Builder $query, $subject, $strict = false) |
|
232 | |||
233 | /* ----------------------------------------------------------------- |
||
234 | | Getters & Setters |
||
235 | | ----------------------------------------------------------------- |
||
236 | */ |
||
237 | /** |
||
238 | * Get the latest_message attribute. |
||
239 | * |
||
240 | * @return \Arcanedev\LaravelMessenger\Models\Message |
||
241 | */ |
||
242 | 9 | public function getLatestMessageAttribute() |
|
246 | |||
247 | /* ----------------------------------------------------------------- |
||
248 | | Main Methods |
||
249 | | ----------------------------------------------------------------- |
||
250 | */ |
||
251 | /** |
||
252 | * Returns all of the latest discussions by `updated_at` date. |
||
253 | * |
||
254 | * @return \Illuminate\Database\Eloquent\Collection |
||
255 | */ |
||
256 | 9 | public static function getLatest() |
|
260 | |||
261 | /** |
||
262 | * Returns all discussions by subject. |
||
263 | * |
||
264 | * @param string $subject |
||
265 | * @param bool $strict |
||
266 | * |
||
267 | * @return \Illuminate\Database\Eloquent\Collection |
||
268 | */ |
||
269 | 18 | public static function getBySubject($subject, $strict = false) |
|
273 | |||
274 | /** |
||
275 | * Returns an array of user ids that are associated with the discussion. |
||
276 | * |
||
277 | * @param int|null $userId |
||
278 | * |
||
279 | * @return \Arcanedev\Support\Collection |
||
280 | */ |
||
281 | 9 | public function participantsUserIds($userId = null) |
|
293 | |||
294 | /** |
||
295 | * Add a user to discussion as a participant. |
||
296 | * |
||
297 | * @param int $userId |
||
298 | * |
||
299 | * @return \Arcanedev\LaravelMessenger\Models\Participant |
||
300 | */ |
||
301 | 126 | public function addParticipant($userId) |
|
311 | |||
312 | /** |
||
313 | * Add users to discussion as participants. |
||
314 | * |
||
315 | * @param array $userIds |
||
316 | * |
||
317 | * @return \Illuminate\Database\Eloquent\Collection |
||
318 | */ |
||
319 | 72 | public function addParticipants(array $userIds) |
|
327 | |||
328 | /** |
||
329 | * Remove a participant from discussion. |
||
330 | * |
||
331 | * @param int $userId |
||
332 | * @param bool $reload |
||
333 | * |
||
334 | * @return int |
||
335 | */ |
||
336 | 18 | public function removeParticipant($userId, $reload = true) |
|
340 | |||
341 | /** |
||
342 | * Remove participants from discussion. |
||
343 | * |
||
344 | * @param array $userIds |
||
345 | * @param bool $reload |
||
346 | * |
||
347 | * @return int |
||
348 | */ |
||
349 | 54 | public function removeParticipants(array $userIds, $reload = true) |
|
360 | |||
361 | /** |
||
362 | * Mark a discussion as read for a user. |
||
363 | * |
||
364 | * @param int $userId |
||
365 | * |
||
366 | * @return bool|int |
||
367 | */ |
||
368 | 45 | public function markAsRead($userId) |
|
378 | |||
379 | /** |
||
380 | * See if the current thread is unread by the user. |
||
381 | * |
||
382 | * @param int $userId |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | 18 | public function isUnread($userId) |
|
392 | |||
393 | /** |
||
394 | * Finds the participant record from a user id. |
||
395 | * |
||
396 | * @param int $userId |
||
397 | * |
||
398 | * @return \Arcanedev\LaravelMessenger\Models\Participant |
||
399 | */ |
||
400 | 63 | public function getParticipantByUserId($userId) |
|
406 | |||
407 | /** |
||
408 | * Get the trashed participants. |
||
409 | * |
||
410 | * @return \Illuminate\Database\Eloquent\Collection |
||
411 | */ |
||
412 | 18 | public function getTrashedParticipants() |
|
416 | |||
417 | /** |
||
418 | * Restores all participants within a discussion. |
||
419 | * |
||
420 | * @param bool $reload |
||
421 | * |
||
422 | * @return int |
||
423 | */ |
||
424 | 9 | public function restoreAllParticipants($reload = true) |
|
436 | |||
437 | /** |
||
438 | * Generates a participant information as a string. |
||
439 | * |
||
440 | * @param int|null $ignoredUserId |
||
441 | * @param \Closure|null $callback |
||
442 | * @param string $glue |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | 18 | public function participantsString($ignoredUserId = null, $callback = null, $glue = ', ') |
|
464 | |||
465 | /** |
||
466 | * Checks to see if a user is a current participant of the discussion. |
||
467 | * |
||
468 | * @param int $userId |
||
469 | * |
||
470 | * @return bool |
||
471 | */ |
||
472 | 18 | public function hasParticipant($userId) |
|
478 | |||
479 | /** |
||
480 | * Returns array of unread messages in discussion for given user. |
||
481 | * |
||
482 | * @param int $userId |
||
483 | * |
||
484 | * @return \Illuminate\Support\Collection |
||
485 | */ |
||
486 | 18 | public function userUnreadMessages($userId) |
|
498 | } |
||
499 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: