DatabaseChannel.php
TLDR
This file contains the DatabaseChannel
class which is responsible for sending notifications to a database. It has two methods: send
and buildPayload
.
Methods
send
This method is used to send a notification. It takes two parameters: $notifiable
which represents the recipient of the notification, and $notification
which is the notification itself. The method returns an instance of \Illuminate\Database\Eloquent\Model
. It sends the notification by calling the routeNotificationFor
method on the $notifiable
object and passing the 'database'
channel and the $notification
as arguments. It then creates a new record in the database with the payload generated by the buildPayload
method.
buildPayload
This method is used to build the payload for the DatabaseNotification
model. It takes two parameters: $notifiable
which represents the recipient of the notification, and $notification
which is the notification itself. The method returns an array containing the following keys: 'id'
, 'type'
, 'data'
, and 'read_at'
. The 'id'
key is set to the same value as the $notification->id
. The 'type'
key is set based on the presence of the databaseType
method on the $notification
object. If the method exists, it is called passing the $notifiable
as an argument, otherwise it is set to the class name of the $notification
object. The 'data'
key is set by calling the getData
method passing $notifiable
and $notification
as arguments. The 'read_at'
key is set to null
.
Class: DatabaseChannel
This class is responsible for sending notifications to a database. It has two methods: send
and buildPayload
.
<?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.');
}
}