diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index bd8af623144f..9f44d08d0dfb 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -55,9 +55,24 @@ protected function buildPayload($notifiable, Notification $notification) { return [ 'id' => $notification->id, - 'type' => get_class($notification), + 'type' => $this->chooseType($notification), 'data' => $this->getData($notifiable, $notification), 'read_at' => null, ]; } + + /** + * Use databaseType method or the notification class name for the type column. + * + * @param mixed $notification + * @return string + */ + private function chooseType($notification) + { + if (method_exists($notification, 'databaseType')) { + return $notification->databaseType(); + } + + return get_class($notification); + } } diff --git a/tests/Notifications/NotificationDatabaseChannelTest.php b/tests/Notifications/NotificationDatabaseChannelTest.php index 9abcedd5393a..92193e8023ce 100644 --- a/tests/Notifications/NotificationDatabaseChannelTest.php +++ b/tests/Notifications/NotificationDatabaseChannelTest.php @@ -49,6 +49,24 @@ public function testCorrectPayloadIsSentToDatabase() $channel = new ExtendedDatabaseChannel; $channel->send($notifiable, $notification); } + + public function testCustomizeTypeIsSentToDatabase() + { + $notification = new NotificationDatabaseChannelCustomizeTypeTestNotification; + $notification->id = 1; + $notifiable = m::mock(); + + $notifiable->shouldReceive('routeNotificationFor->create')->with([ + 'id' => 1, + 'type' => 'MONTHLY', + 'data' => ['invoice_id' => 1], + 'read_at' => null, + 'something' => 'else', + ]); + + $channel = new ExtendedDatabaseChannel; + $channel->send($notifiable, $notification); + } } class NotificationDatabaseChannelTestNotification extends Notification @@ -59,6 +77,19 @@ public function toDatabase($notifiable) } } +class NotificationDatabaseChannelCustomizeTypeTestNotification extends Notification +{ + public function toDatabase($notifiable) + { + return new DatabaseMessage(['invoice_id' => 1]); + } + + public function databaseType() + { + return 'MONTHLY'; + } +} + class ExtendedDatabaseChannel extends DatabaseChannel { protected function buildPayload($notifiable, Notification $notification)