Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Illuminate/Broadcasting/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/BroadcastsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down