diff --git a/README.md b/README.md index 89892ab..98ccbe0 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,26 @@ class WelcomeMail extends TemplateMailable By extending the `\Spatie\MailTemplates\TemplateMailable` class this mailable will be rendered using the corresponding `MailTemplate`. All public properties on the `WelcomeMail` will be available in the template. +If you need to use properties within your template that are initially defined within your `WelcomeMail` (for example, data that comes from another source). You can call `$this->setAdditionalData()` and pass it an array of you additional key => value pairs. + +An example of this would be: +```php +namespace App\Mail; + +use TemplateMailable; + +class WelcomeMail extends TemplateMailable +{ + + public function __construct(User $user) + { + $this->setAdditionalData([ + 'name' => 'Joe Bloggs' + ]); + } +} +``` + ### Customizing the `MailTemplate` model The default `MailTemplate` model is sufficient for using _one_ database mail template for _one_ mailable. If you want to use multiple mail templates for the same mailable _or_ extend the `MailTemplate` model, we highly encourage you to publish the `mail_template` migration and create your own mail template model by extending `MailTemplate`. Make sure to implement the `MailTemplateInterface` interface as well. diff --git a/src/TemplateMailable.php b/src/TemplateMailable.php index d28f107..6b96b7f 100644 --- a/src/TemplateMailable.php +++ b/src/TemplateMailable.php @@ -17,6 +17,9 @@ abstract class TemplateMailable extends Mailable /** @var MailTemplateInterface */ protected $mailTemplate; + /** @var array */ + protected $additionalData; + public static function getVariables(): array { return static::getPublicProperties(); @@ -111,4 +114,14 @@ protected function getMailTemplateRenderer(): TemplateMailableRenderer { return app(TemplateMailableRenderer::class, ['templateMailable' => $this]); } + + public function buildViewData(): array + { + $mailablePropeties = parent::buildViewData(); + return array_merge($mailablePropeties, $this->additionalData ?? []); + } + + public function setAdditionalData($array) { + $this->additionalData = $array; + } }