Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add forceDeleting event #45836

Merged
merged 3 commits into from
Jan 27, 2023
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getObservableEvents()
[
'retrieved', 'creating', 'created', 'updating', 'updated',
'saving', 'saved', 'restoring', 'restored', 'replicating',
'deleting', 'deleted', 'forceDeleted',
'deleting', 'deleted', 'forceDeleting', 'forceDeleted',
],
$this->observables
);
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function initializeSoftDeletes()
*/
public function forceDelete()
{
if ($this->fireModelEvent('forceDeleting') === false) {
return false;
}

$this->forceDeleting = true;

return tap($this->delete(), function ($deleted) {
Expand Down Expand Up @@ -191,6 +195,17 @@ public static function restored($callback)
static::registerModelEvent('restored', $callback);
}

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

/**
* Register a "forceDeleted" model event callback with the dispatcher.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public function testForceDeletedEventIsFired()
$this->assertEquals($role->id, RoleObserver::$model->id);
}

public function testForceDeletingEventIsFired()
{
$role = Role::create([]);
$this->assertInstanceOf(Role::class, $role);
Role::observe(new RoleObserver());

$role->forceDelete();

$this->assertEquals($role->id, RoleObserver::$model->id);
}

public function testDeleteQuietly()
{
$_SERVER['(-_-)'] = '\(^_^)/';
Expand Down