DatabaseChannel.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Illuminate\Notifications\Channels;
use Illuminate\Notifications\Notification;
use RuntimeException;
class DatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database', $notification)->create(
$this->buildPayload($notifiable, $notification)
);
}
/**
* Build an array payload for the DatabaseNotification Model.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function buildPayload($notifiable, Notification $notification)
{
return [
'id' => $notification->id,
'type' => method_exists($notification, 'databaseType')
? $notification->databaseType($notifiable)
: get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
];
}
/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*
* @throws \RuntimeException
*/
protected function getData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toDatabase')) {
return is_array($data = $notification->toDatabase($notifiable))
? $data : $data->data;
}
if (method_exists($notification, 'toArray')) {
return $notification->toArray($notifiable);
}
throw new RuntimeException('Notification is missing toDatabase / toArray method.');
}
}