From 69f7e7ee6a93a5d1d0b8fed9d86e6f629309e3da Mon Sep 17 00:00:00 2001 From: mahdihty Date: Mon, 15 Mar 2021 14:53:30 +0330 Subject: [PATCH] add business_id to DBNotification.php overwrite database notif channel --- app/Channels/DBChannel.php | 45 ++++++++++++++++++++++++++++ app/Notifications/DBNotification.php | 15 ++++++++-- app/Providers/AppServiceProvider.php | 4 +++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/Channels/DBChannel.php diff --git a/app/Channels/DBChannel.php b/app/Channels/DBChannel.php new file mode 100644 index 0000000..57452ba --- /dev/null +++ b/app/Channels/DBChannel.php @@ -0,0 +1,45 @@ +toDatabaseOptional($notifiable)) + ? $data : [$data]; + } + } + + /** + * 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' => get_class($notification), + 'data' => $this->getData($notifiable, $notification), + 'read_at' => null, + ] + $this->getOptionalData($notifiable, $notification); + } +} diff --git a/app/Notifications/DBNotification.php b/app/Notifications/DBNotification.php index ad3202b..1711b56 100644 --- a/app/Notifications/DBNotification.php +++ b/app/Notifications/DBNotification.php @@ -29,7 +29,7 @@ class DBNotification extends Notification */ public function via($notifiable) { - return ['database']; + return ['db']; } /** @@ -42,7 +42,18 @@ class DBNotification extends Notification { return [ 'data' => $this->message['body'], - 'business_id' => 1 + ]; + } + + /** + * Get the array for optional database field that added to original table. + * @param $notifiable + * @return array + */ + public function toDatabaseOptional($notifiable) + { + return [ + 'business_id' => $this->message['business_id'] ?? null ]; } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 281bc7c..d25685e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Channels\DBChannel; use App\Channels\FcmChannel; use App\Channels\SocketChannel; use Illuminate\Notifications\ChannelManager; @@ -25,6 +26,9 @@ class AppServiceProvider extends ServiceProvider $service->extend('socket', function ($app) { return new SocketChannel(new HttpClient, config('socket.url')); }); + $service->extend('db', function ($app) { + return new DBChannel(); + }); }); }