Skip to content

Commit 959e111

Browse files
authored
Update notifications.md
1 parent 65c2e33 commit 959e111

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

notifications.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,29 @@ In addition, you may return a full [mailable object](/docs/{{version}}/mail) fro
308308
->to($notifiable->email);
309309
}
310310

311+
When using a `Mailable` instead of a `MailMessage`, the receiver should be explicitly defined on the `Mailable` itself. [Overwriting `routeNotificationForMail`](#customizing-the-recipient) on an Eloquent model or using [on-demand notifications](#on-demand-notifications) don't allow you to specify the receiver in this case. Instead you should always use the `to` method on the `Mailable` to define the receiver based on the `$notifiable`.
312+
313+
The above example already shows how this can be done for regular notifiable models. For on-demand notifications you may derive the email address(es) by checking the `AnonymousNotifiable` type:
314+
315+
use App\Mail\InvoicePaid as InvoicePaidMailable;
316+
use Illuminate\Notifications\AnonymousNotifiable;
317+
318+
/**
319+
* Get the mail representation of the notification.
320+
*
321+
* @param mixed $notifiable
322+
* @return Mailable
323+
*/
324+
public function toMail($notifiable)
325+
{
326+
$recipient = $notifiable instanceof AnonymousNotifiable
327+
? $notifiable->routeNotificationFor('mail')
328+
: $notifiable->email;
329+
330+
return (new InvoicePaidMailable($this->invoice))
331+
->to($recipient);
332+
}
333+
311334
<a name="error-messages"></a>
312335
#### Error Messages
313336

0 commit comments

Comments
 (0)