Notifications
Set the notifiable that receives a notification.
Set the notifiable
Pass a closure that returns the notifiable to withNotifiable():
php
use App\Models\Invoice;
use App\Models\User;
use HosmelQ\MessagePreview\Support\PreviewConfig;
use Illuminate\Notifications\Notification;
final class InvoicePaid extends Notification
{
public function __construct(public Invoice $invoice) {}
public static function previewConfig(): PreviewConfig
{
return PreviewConfig::configure()
->withMessage(fn (): self => new self(Invoice::factory()->make()))
->withNotifiable(fn (): User => User::factory()->make());
}
}The notifiable needs a mail route, such as a model that uses the Notifiable trait or an AnonymousNotifiable:
php
->withNotifiable(fn (): AnonymousNotifiable => new AnonymousNotifiable()->route('mail', 'ana@example.com'))The closure receives the notification, so the notifiable can come from its data:
php
->withNotifiable(fn (self $notification): User => $notification->invoice->customer)Calling withNotifiable() on a mailable fails with a configuration error.
Override it for a variant
Pass a notifiable closure as the third argument of withVariant():
php
->withVariant(
'team-invoice',
fn (): self => new self(Invoice::factory()->forTeam()->make()),
fn (self $notification): User => $notification->invoice->team->owner,
)Locale
The notification renders in its own locale. Without one, it uses the preferred locale of the notifiable, then the application locale.