|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Models\Notification; |
|
6
|
|
|
|
|
7
|
|
|
trait Notifable |
|
8
|
|
|
{ |
|
9
|
|
|
public function notifications() |
|
10
|
|
|
{ |
|
11
|
|
|
$model = notifynder_config()->getNotificationModel(); |
|
12
|
|
|
if (notifynder_config()->isPolymorphic()) { |
|
13
|
|
|
return $this->morphMany($model, 'to'); |
|
|
|
|
|
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
return $this->hasMany($model, 'to_id'); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function notifynder($category) |
|
20
|
|
|
{ |
|
21
|
|
|
return app('notifynder')->category($category); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function sendNotificationFrom($category) |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->notifynder($category)->from($this); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function sendNotificationTo($category) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->notifynder($category)->to($this); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function readNotification($notification) |
|
35
|
|
|
{ |
|
36
|
|
|
if (! ($notification instanceof Notification)) { |
|
37
|
|
|
$notification = Notification::firstOrFail($notification); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $notification->read(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function unreadNotification($notification) |
|
44
|
|
|
{ |
|
45
|
|
|
if (! ($notification instanceof Notification)) { |
|
46
|
|
|
$notification = Notification::firstOrFail($notification); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $notification->unread(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function countUnreadNotifications() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->notifications()->byRead(0)->count(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getNotifications($limit = null, $order = 'desc') |
|
58
|
|
|
{ |
|
59
|
|
|
$query = $this->notifications()->orderBy('created_at', $order); |
|
60
|
|
|
if(!is_null($limit)) { |
|
61
|
|
|
$query->limit($limit); |
|
62
|
|
|
} |
|
63
|
|
|
return $query->get(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
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.