diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 19422a287893..fdf12ee34722 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -132,6 +132,13 @@ class Mailable implements MailableContract, Renderable */ public $callbacks = []; + /** + * The callback that should be invoked while building the view data. + * + * @var callable + */ + public static $viewDataCallback; + /** * Send the message using the given mailer. * @@ -263,6 +270,10 @@ public function buildViewData() { $data = $this->viewData; + if (static::$viewDataCallback) { + $data = array_merge($data, call_user_func(static::$viewDataCallback, $this)); + } + foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { if ($property->getDeclaringClass()->getName() !== self::class) { $data[$property->getName()] = $property->getValue($this); @@ -793,6 +804,17 @@ public function withSwiftMessage($callback) return $this; } + /** + * Register a callback to be called while building the view data. + * + * @param callable $callback + * @return void + */ + public static function buildViewDataUsing(callable $callback) + { + static::$viewDataCallback = $callback; + } + /** * Dynamically bind parameters to the message. *