简体   繁体   中英

Laravel - should send notification method

I have this configuration where the user can set which notification it wants to receive, I don't want to do an If on every method that I call notify .

I'd like to know if is there a method inside my notification class that I can do this validation, or how could I do that.

I thought about a solution, but it seens durty, I could validate inside via and just return an empty array if the user setted to not receive

And I also find out a method inside Illuminate\Notifications\NotificationSender called shouldSendNotification but I don't know how I could overwrite it, or even if it is using this class, cause it seens to be only for queue

OBS: Laravel 7

via would previously have been the best place for this, but Laravel now supports shouldSend on the notification for exactly this behaviour.

/**
 * Determine if the notification should be sent.
 *
 * @param  mixed  $notifiable
 * @param  string  $channel
 * @return bool|null
 */
public function shouldSend($notifiable, $channel)
{
    if ($user->isUnsubscribed()) {
        return false;
    }
}

I guess the easiest way is to define a wrapper method in your User model and use it instead of notify like this:

public function _notify($notification)
{
    if ($this->notify_condition) {
        $this->notify($notification);
    }
}

now you use _notify instead of notify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM