|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMessenger\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMessenger\Models; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait Messagable |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanedev\LaravelMessenger\Traits |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @property int id |
|
17
|
|
|
* @property \Illuminate\Database\Eloquent\Collection discussions |
|
18
|
|
|
* @property \Illuminate\Database\Eloquent\Collection participations |
|
19
|
|
|
* @property \Illuminate\Database\Eloquent\Collection messages |
|
20
|
|
|
*/ |
|
21
|
|
|
trait Messagable |
|
22
|
|
|
{ |
|
23
|
|
|
/* ----------------------------------------------------------------- |
|
24
|
|
|
| Relationships |
|
25
|
|
|
| ----------------------------------------------------------------- |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Thread relationship. |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
32
|
|
|
*/ |
|
33
|
12 |
|
public function discussions() |
|
34
|
|
|
{ |
|
35
|
12 |
|
return $this->morphToMany( |
|
|
|
|
|
|
36
|
12 |
|
config('messenger.discussions.model', Models\Discussion::class), |
|
37
|
12 |
|
config('messenger.users.morph', 'participable'), |
|
38
|
12 |
|
config('messenger.participations.table', 'participations') |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Participations relationship. |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function participations() |
|
48
|
|
|
{ |
|
49
|
6 |
|
return $this->morphMany( |
|
|
|
|
|
|
50
|
6 |
|
config('messenger.participations.model', Models\Participation::class), |
|
51
|
6 |
|
config('messenger.users.morph', 'participable') |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Message relationship. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function messages() |
|
61
|
|
|
{ |
|
62
|
6 |
|
return $this->morphMany( |
|
|
|
|
|
|
63
|
6 |
|
config('messenger.messages.model', Models\Message::class), |
|
64
|
6 |
|
config('messenger.users.morph', 'participable') |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* ----------------------------------------------------------------- |
|
69
|
|
|
| Main Methods |
|
70
|
|
|
| ----------------------------------------------------------------- |
|
71
|
|
|
*/ |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the new messages count for user. |
|
75
|
|
|
* |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function newMessagesCount() |
|
79
|
|
|
{ |
|
80
|
6 |
|
return $this->discussionsWithNewMessages()->count(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns all discussions IDs with new messages. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
87
|
|
|
*/ |
|
88
|
6 |
|
public function discussionsWithNewMessages() |
|
89
|
|
|
{ |
|
90
|
6 |
|
$participationsTable = config('messenger.participations.table', 'participations'); |
|
91
|
6 |
|
$discussionsTable = config('messenger.discussions.table', 'discussions'); |
|
92
|
|
|
|
|
93
|
|
|
return $this->discussions()->where(function (Builder $query) use ($participationsTable, $discussionsTable) { |
|
94
|
6 |
|
$query->whereNull("$participationsTable.last_read"); |
|
95
|
6 |
|
$query->orWhere( |
|
96
|
6 |
|
"$discussionsTable.updated_at", '>', $this->getConnection()->raw("$participationsTable.last_read") |
|
|
|
|
|
|
97
|
|
|
); |
|
98
|
6 |
|
})->get(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.