Skip to content

Commit

Permalink
fire a trashed event on soft deleting and listen to it for model bro…
Browse files Browse the repository at this point in the history
…adcasting (#37618)
  • Loading branch information
themsaid authored Jun 7, 2021
1 parent 1e06ebe commit 002595d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Eloquent/BroadcastableModelEventOccurred.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ public function onChannels(array $channels)

return $this;
}

/**
* Get the event name.
*
* @return string
*/
public function event()
{
return $this->event;
}
}
2 changes: 1 addition & 1 deletion Eloquent/BroadcastsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function bootBroadcastsEvents()
});

if (method_exists(static::class, 'bootSoftDeletes')) {
static::trashed(function ($model) {
static::softDeleted(function ($model) {
$model->broadcastTrashed();
});

Expand Down
13 changes: 13 additions & 0 deletions Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected function runSoftDelete()
$query->update($columns);

$this->syncOriginalAttributes(array_keys($columns));

$this->fireModelEvent('trashed', false);
}

/**
Expand Down Expand Up @@ -136,6 +138,17 @@ public function trashed()
return ! is_null($this->{$this->getDeletedAtColumn()});
}

/**
* Register a "softDeleted" model event callback with the dispatcher.
*
* @param \Closure|string $callback
* @return void
*/
public static function softDeleted($callback)
{
static::registerModelEvent('trashed', $callback);
}

/**
* Register a "restoring" model event callback with the dispatcher.
*
Expand Down

0 comments on commit 002595d

Please sign in to comment.