mercredi 27 juin 2018

Pass data between different "Via" Laravel Notification

Do you know how to pass data between 2 different "Via" notification? This is the situation: I have created 2 custom channels for notification

  1. PushNotificationChannel
  2. CustomDatabase

In my Notification Class I have this:

class GeneralNotification extends Notification
{
    use Queueable;

    private $notificationType;
    private $sender;

    public function __construct($notificationType, $sender)
    {
        $this->notificationType = $notificationType;
        $this->sender = $sender;
    }


    public function via($notifiable)
    {
        return [CustomDatabaseChannel::class, PushNotificationChannel::class];
    }

    public function toPushNotification($notifiable)
    {
        return [
            //
        ];
    }


    public function toCustomDatabase($notifiable)
    {
       return [
            //
        ];
    }
}

So this will first execute the toCustomDatabase method, and then the toPushNotification method.

What I want is after I save the data into de database, pass it (the inserted record) to the toPushNotification method.

I was testing with assigning for example:

public function toCustomDatabase($notifiable)
    {
       $this->notificationType = 'test';
       return [
            //
        ];
    }

but when I do:

public function toPushNotification($notifiable)
    {
        dd($this->notificationType);
        return [
            //
        ];
    }

It shows me the original notificationType, and not the value I changed in the toCustomDatabase method.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire