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 readAllNotifications() |
44
|
|
|
{ |
45
|
|
|
return $this->notifications()->update(['read' => 1]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function unreadNotification($notification) |
49
|
|
|
{ |
50
|
|
|
if (! ($notification instanceof Notification)) { |
51
|
|
|
$notification = Notification::firstOrFail($notification); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $notification->unread(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function countUnreadNotifications() |
58
|
|
|
{ |
59
|
|
|
return $this->notifications()->byRead(0)->count(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getNotifications($limit = null, $order = 'desc') |
63
|
|
|
{ |
64
|
|
|
$query = $this->notifications()->orderBy('created_at', $order); |
65
|
|
|
if (! is_null($limit)) { |
66
|
|
|
$query->limit($limit); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $query->get(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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
Idable
provides a methodequalsId
that 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.