From c9bb79958a5b96d37bfc051638cc6cd575382a39 Mon Sep 17 00:00:00 2001 From: LewBlu Date: Tue, 4 Jun 2024 20:36:30 +0100 Subject: [PATCH 1/2] Added overload function for allowing dynamic properties --- README.md | 20 ++++++++++++++++++++ src/TemplateMailable.php | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) 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..15bcbd8 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,17 @@ protected function getMailTemplateRenderer(): TemplateMailableRenderer { return app(TemplateMailableRenderer::class, ['templateMailable' => $this]); } + + public function buildViewData(): array + { + $mailablePropeties = parent::buildViewData(); + if(is_null($this->additionalData)) { + return $mailablePropeties; + } + return array_merge($mailablePropeties, $this->additionalData); + } + + public function setAdditionalData($array) { + $this->additionalData = $array; + } } From 55e69588820882fc6908af8997f853def7405ca5 Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Thu, 3 Oct 2024 16:37:35 +0200 Subject: [PATCH 2/2] Update src/TemplateMailable.php --- src/TemplateMailable.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/TemplateMailable.php b/src/TemplateMailable.php index 15bcbd8..6b96b7f 100644 --- a/src/TemplateMailable.php +++ b/src/TemplateMailable.php @@ -118,10 +118,7 @@ protected function getMailTemplateRenderer(): TemplateMailableRenderer public function buildViewData(): array { $mailablePropeties = parent::buildViewData(); - if(is_null($this->additionalData)) { - return $mailablePropeties; - } - return array_merge($mailablePropeties, $this->additionalData); + return array_merge($mailablePropeties, $this->additionalData ?? []); } public function setAdditionalData($array) {