Back

Check user settings before sending an email notification


brunobg posted 2 weeks ago

I have a receiveEmail boolean field in the User model of a Laravel application. How do I ensure that mail notifications respect this field, and only sends the email to the user if the field is true?

What I want is that this code:

$event = new SomeEvent($somedata);
Auth::user()->notify($event);

or this one

Notification::send($users, $event);

where SomeEvent is a class that extends Notification and implements 'mail' on the via() method, only sends an email if the user has allowed emails.

Cameron replied 2 weeks ago

If auth user receiveEmail ?

if(Auth::user()->recieveEmail{
Auth::user()-notify($event);
}
Cameron replied 2 weeks ago

You could also do the check in the event itself and pass user I’d to the event along with the somedata


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us