2

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

2 Answers 2

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.