diff --git a/src/Illuminate/Broadcasting/BroadcastManager.php b/src/Illuminate/Broadcasting/BroadcastManager.php index 833a19948b8a..a4957cde900f 100644 --- a/src/Illuminate/Broadcasting/BroadcastManager.php +++ b/src/Illuminate/Broadcasting/BroadcastManager.php @@ -112,7 +112,10 @@ public function event($event = null) */ public function queue($event) { - if ($event instanceof ShouldBroadcastNow) { + if ($event instanceof ShouldBroadcastNow || + (is_object($event) && + method_exists($event, 'shouldBroadcastNow') && + $event->shouldBroadcastNow())) { return $this->app->make(BusDispatcherContract::class)->dispatchNow(new BroadcastEvent(clone $event)); } diff --git a/src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php b/src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php index 9e2c3dc82f98..14be425afa43 100644 --- a/src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php +++ b/src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php @@ -114,6 +114,17 @@ public function onChannels(array $channels) return $this; } + /** + * Determine if the event should be broadcast synchronously. + * + * @return bool + */ + public function shouldBroadcastNow() + { + return $this->event === 'deleted' && + ! method_exists($this->model, 'bootSoftDeletes'); + } + /** * Get the event name. * diff --git a/src/Illuminate/Database/Eloquent/BroadcastsEvents.php b/src/Illuminate/Database/Eloquent/BroadcastsEvents.php index 008fb128afce..79dc02d8aea7 100644 --- a/src/Illuminate/Database/Eloquent/BroadcastsEvents.php +++ b/src/Illuminate/Database/Eloquent/BroadcastsEvents.php @@ -111,6 +111,10 @@ public function broadcastDeleted($channels = null) */ protected function broadcastIfBroadcastChannelsExistForEvent($instance, $event, $channels = null) { + if (! static::$isBroadcasting) { + return; + } + if (! empty($this->broadcastOn($event)) || ! empty($channels)) { return broadcast($instance->onChannels(Arr::wrap($channels))); } diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 2222ffe1a46e..0ce4a0e2dff4 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -166,6 +166,13 @@ abstract class Model implements Arrayable, ArrayAccess, HasBroadcastChannel, Jso */ protected static $lazyLoadingViolationCallback; + /** + * Indicates if broadcasting is currently enabled. + * + * @var bool + */ + protected static $isBroadcasting = true; + /** * The name of the "created at" column. * @@ -377,6 +384,25 @@ public static function handleLazyLoadingViolationUsing(callable $callback) static::$lazyLoadingViolationCallback = $callback; } + /** + * Execute a callback without broadcasting any model events for all model types. + * + * @param callable $callback + * @return mixed + */ + public static function withoutBroadcasting(callable $callback) + { + $isBroadcasting = static::$isBroadcasting; + + static::$isBroadcasting = false; + + try { + return $callback(); + } finally { + static::$isBroadcasting = $isBroadcasting; + } + } + /** * Fill the model with an array of attributes. *