Browse Source

add business_id to DBNotification.php

overwrite database notif channel
master
mahdihty 4 years ago
parent
commit
69f7e7ee6a
  1. 45
      app/Channels/DBChannel.php
  2. 15
      app/Notifications/DBNotification.php
  3. 4
      app/Providers/AppServiceProvider.php

45
app/Channels/DBChannel.php

@ -0,0 +1,45 @@
<?php
namespace App\Channels;
use Illuminate\Notifications\Channels\DatabaseChannel;
use Illuminate\Notifications\Notification;
class DBChannel extends DatabaseChannel
{
/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*
* @throws \RuntimeException
*/
protected function getOptionalData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toDatabaseOptional')) {
return is_array($data = $notification->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);
}
}

15
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
];
}
}

4
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();
});
});
}

Loading…
Cancel
Save