|
1
|
|
|
<?php namespace Arcanedev\LaravelMessenger\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMessenger\Models; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Trait Messagable |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\LaravelMessenger\Traits |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @property int id |
|
13
|
|
|
* @property \Illuminate\Database\Eloquent\Collection discussions |
|
14
|
|
|
* @property \Illuminate\Database\Eloquent\Collection participations |
|
15
|
|
|
* @property \Illuminate\Database\Eloquent\Collection messages |
|
16
|
|
|
*/ |
|
17
|
|
|
trait Messagable |
|
18
|
|
|
{ |
|
19
|
|
|
/* ----------------------------------------------------------------- |
|
20
|
|
|
| Traits |
|
21
|
|
|
| ----------------------------------------------------------------- |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
use ConfigHelper; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Relationships |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Thread relationship. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany |
|
35
|
|
|
*/ |
|
36
|
6 |
|
public function discussions() |
|
37
|
|
|
{ |
|
38
|
6 |
|
return $this->morphToMany( |
|
|
|
|
|
|
39
|
6 |
|
$this->getModelFromConfig('discussions', Models\Discussion::class), |
|
40
|
6 |
|
'participable', |
|
41
|
6 |
|
$this->getTableFromConfig('participations', 'participations') |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Participations relationship. |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function participations() |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->morphMany( |
|
|
|
|
|
|
53
|
3 |
|
$this->getModelFromConfig('participations', Models\Participation::class), |
|
54
|
3 |
|
'participable' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Message relationship. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function messages() |
|
64
|
|
|
{ |
|
65
|
3 |
|
return $this->morphMany( |
|
|
|
|
|
|
66
|
3 |
|
$this->getModelFromConfig('messages', Models\Message::class), |
|
67
|
3 |
|
'participable' |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/* ----------------------------------------------------------------- |
|
72
|
|
|
| Main Methods |
|
73
|
|
|
| ----------------------------------------------------------------- |
|
74
|
|
|
*/ |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the new messages count for user. |
|
78
|
|
|
* |
|
79
|
|
|
* @return int |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function newMessagesCount() |
|
82
|
|
|
{ |
|
83
|
3 |
|
return $this->discussionsWithNewMessages()->count(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns all discussions IDs with new messages. |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function discussionsWithNewMessages() |
|
92
|
|
|
{ |
|
93
|
3 |
|
$participationsTable = $this->getTableFromConfig('participations', 'participations'); |
|
94
|
3 |
|
$discussionsTable = $this->getTableFromConfig('discussions', 'discussions'); |
|
95
|
|
|
|
|
96
|
3 |
|
return $this->discussions()->where(function (Builder $query) use ($participationsTable, $discussionsTable) { |
|
97
|
3 |
|
$query->whereNull("$participationsTable.last_read"); |
|
98
|
3 |
|
$query->orWhere( |
|
99
|
3 |
|
"$discussionsTable.updated_at", '>', $this->getConnection()->raw("$participationsTable.last_read") |
|
|
|
|
|
|
100
|
|
|
); |
|
101
|
3 |
|
})->get(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/* ----------------------------------------------------------------- |
|
105
|
|
|
| Eloquent Methods |
|
106
|
|
|
| ----------------------------------------------------------------- |
|
107
|
|
|
*/ |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Define a many-to-many relationship. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $related |
|
113
|
|
|
* @param string $table |
|
114
|
|
|
* @param string $foreignPivotKey |
|
115
|
|
|
* @param string $relatedPivotKey |
|
116
|
|
|
* @param string $parentKey |
|
117
|
|
|
* @param string $relatedKey |
|
118
|
|
|
* @param string $relation |
|
119
|
|
|
* |
|
120
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
121
|
|
|
*/ |
|
122
|
|
|
abstract public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
|
123
|
|
|
$parentKey = null, $relatedKey = null, $relation = null); |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Define a one-to-many relationship. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $related |
|
129
|
|
|
* @param string $foreignKey |
|
130
|
|
|
* @param string $localKey |
|
131
|
|
|
* |
|
132
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
133
|
|
|
*/ |
|
134
|
|
|
abstract public function hasMany($related, $foreignKey = null, $localKey = null); |
|
135
|
|
|
} |
|
136
|
|
|
|
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.