From 3a5e510af5e92ab2eaa25d728b8c74d9cf8833c2 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Sep 2016 16:38:49 -0500 Subject: [PATCH] Format notifiable properly. --- .../Notifications/ChannelManager.php | 30 +++++++++++++------ .../Notifications/RoutesNotifications.php | 2 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Notifications/ChannelManager.php b/src/Illuminate/Notifications/ChannelManager.php index d89b16ad6194..d8f31084aa37 100644 --- a/src/Illuminate/Notifications/ChannelManager.php +++ b/src/Illuminate/Notifications/ChannelManager.php @@ -8,9 +8,11 @@ use Nexmo\Client as NexmoClient; use Illuminate\Support\Collection; use GuzzleHttp\Client as HttpClient; +use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Bus\Dispatcher as Bus; use Nexmo\Client\Credentials\Basic as NexmoCredentials; +use Illuminate\Database\Eloquent\Collection as ModelCollection; use Illuminate\Contracts\Notifications\Factory as FactoryContract; use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract; @@ -32,9 +34,7 @@ class ChannelManager extends Manager implements DispatcherContract, FactoryContr */ public function send($notifiables, $notification) { - if (! $notifiables instanceof Collection && ! is_array($notifiables)) { - $notifiables = [$notifiables]; - } + $notifiables = $this->formatNotifiables($notifiables); if ($notification instanceof ShouldQueue) { return $this->queueNotification($notifiables, $notification); @@ -52,9 +52,7 @@ public function send($notifiables, $notification) */ public function sendNow($notifiables, $notification, array $channels = null) { - if (! $notifiables instanceof Collection && ! is_array($notifiables)) { - $notifiables = [$notifiables]; - } + $notifiables = $this->formatNotifiables($notifiables); $original = clone $notification; @@ -109,9 +107,7 @@ protected function shouldSendNotification($notifiable, $notification, $channel) */ protected function queueNotification($notifiables, $notification) { - if (! $notifiables instanceof Collection && ! is_array($notifiables)) { - $notifiables = [$notifiables]; - } + $notifiables = $this->formatNotifiables($notifiables); $bus = $this->app->make(Bus::class); @@ -127,6 +123,22 @@ protected function queueNotification($notifiables, $notification) } } + /** + * Format the notifiables into a Collection / array if necessary. + * + * @param mixed $notifiables + * @return ModelCollection|array + */ + protected function formatNotifiables($notifiables) + { + if (! $notifiables instanceof Collection && ! is_array($notifiables)) { + return $notifiables instanceof Model + ? new ModelCollection([$notifiables]) : [$notifiables]; + } + + return $notifiables; + } + /** * Get a channel instance. * diff --git a/src/Illuminate/Notifications/RoutesNotifications.php b/src/Illuminate/Notifications/RoutesNotifications.php index f73cdffc1571..bedbe96ea211 100644 --- a/src/Illuminate/Notifications/RoutesNotifications.php +++ b/src/Illuminate/Notifications/RoutesNotifications.php @@ -15,7 +15,7 @@ trait RoutesNotifications */ public function notify($instance) { - app(Dispatcher::class)->send([$this], $instance); + app(Dispatcher::class)->send($this, $instance); } /**