Sending Laravel Notifications Without Notifiable
Arie Visser • May 14, 2021laravel php
Notifications in Laravel are typically used to notify users in your application.
Users do not have to be the
App\Models\User model, but can be any model that includes the
However, sometimes there is no notifiable model involved, and you need to send a notification directly to a given email address, or a list of addresses.
When you try to provide an email address to the
send method of the
Notification facade, like this:
Notification::send('firstname.lastname@example.org', new MyNotification());
You will get the following error message:
TypeError : get_class(): Argument #1 ($object) must be of type object, string given
Luckily, the Laravel Framework has a solution for this, with on-demand notifications, that can be written like this:
Notification::route('mail', 'email@example.com') ->notify(new MyNotification());
As you can see, the notification object
MyNotification is used, but without a model that includes the Notifiable trait.
It is also possible to provide an array of recipients:
Notification::route('mail', ['firstname.lastname@example.org', 'email@example.com']) ->notify(new MyNotification());