From bc5bd660b0a3079e3fb7f392bfac06e41d80b64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20M=C3=B6ller?= <63605405+TobMoeller@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:29:06 +0200 Subject: [PATCH] add mailable class metadata to viewdata (#53042) Co-authored-by: TobMoeller --- src/Illuminate/Mail/Mailable.php | 14 +++++++++++++- tests/Mail/MailMailableDataTest.php | 8 ++++++-- tests/Mail/MailMailableTest.php | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 48831c0e551a..b1ac21358009 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -352,7 +352,19 @@ public function buildViewData() } } - return $data; + return array_merge($data, $this->additionalMessageData()); + } + + /** + * Get additional meta-data to pass along with the view data. + * + * @return array + */ + protected function additionalMessageData(): array + { + return [ + '__laravel_mailable' => get_class($this), + ]; } /** diff --git a/tests/Mail/MailMailableDataTest.php b/tests/Mail/MailMailableDataTest.php index 0aa0aa21e3b9..01737705b033 100644 --- a/tests/Mail/MailMailableDataTest.php +++ b/tests/Mail/MailMailableDataTest.php @@ -9,9 +9,13 @@ class MailMailableDataTest extends TestCase { public function testMailableDataIsNotLost() { - $testData = ['first_name' => 'James']; - $mailable = new MailableStub; + + $testData = [ + 'first_name' => 'James', + '__laravel_mailable' => get_class($mailable), + ]; + $mailable->build(function ($m) use ($testData) { $m->view('view', $testData); }); diff --git a/tests/Mail/MailMailableTest.php b/tests/Mail/MailMailableTest.php index 9c21f8b06dc8..f610ec80262e 100644 --- a/tests/Mail/MailMailableTest.php +++ b/tests/Mail/MailMailableTest.php @@ -592,6 +592,7 @@ public function testMailableBuildsViewData() 'first_name' => 'Taylor', 'lastName' => 'Otwell', 'framework' => 'Laravel', + '__laravel_mailable' => get_class($mailable), ]; $this->assertSame($expected, $mailable->buildViewData());