diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php index 0972bd6fec33..14d7a5eefaa2 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php @@ -55,9 +55,9 @@ public function getObservableEvents() { return array_merge( [ - 'retrieved', 'creating', 'created', 'updating', - 'updated', 'deleting', 'deleted', 'saving', - 'saved', 'restoring', 'restored', + 'retrieved', 'creating', 'created', 'updating', 'updated', + 'saving', 'saved', 'restoring', 'restored', + 'deleting', 'deleted', 'forceDeleted', ], $this->observables ); diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index 74ba0622bf65..32b60fdcc1af 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -30,11 +30,13 @@ public function forceDelete() { $this->forceDeleting = true; - $deleted = $this->delete(); + return tap($this->delete(), function ($deleted) { + $this->forceDeleting = false; - $this->forceDeleting = false; - - return $deleted; + if ($deleted) { + $this->fireModelEvent('forceDeleted', false); + } + }); } /** diff --git a/tests/Integration/Database/EloquentDeleteTest.php b/tests/Integration/Database/EloquentDeleteTest.php index 1c21e082c582..fddcf8c2ae11 100644 --- a/tests/Integration/Database/EloquentDeleteTest.php +++ b/tests/Integration/Database/EloquentDeleteTest.php @@ -5,6 +5,7 @@ use Orchestra\Testbench\TestCase; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; /** * @group integration @@ -40,6 +41,12 @@ public function setUp() $table->integer('post_id'); $table->timestamps(); }); + + Schema::create('roles', function ($table) { + $table->increments('id'); + $table->timestamps(); + $table->softDeletes(); + }); } public function testOnlyDeleteWhatGiven() @@ -56,6 +63,20 @@ public function testOnlyDeleteWhatGiven() Post::join('comments', 'comments.post_id', '=', 'posts.id')->where('posts.id', '>', 1)->orderBy('posts.id')->limit(1)->delete(); $this->assertEquals(8, Post::all()->count()); } + + public function testForceDeletedEventIsFired() + { + $role = Role::create([]); + $this->assertInstanceOf(Role::class, $role); + Role::observe(new RoleObserver); + + $role->delete(); + $this->assertNull(RoleObserver::$model); + + $role->forceDelete(); + + $this->assertEquals($role->id, RoleObserver::$model->id); + } } class Post extends Model @@ -68,3 +89,20 @@ class Comment extends Model public $table = 'comments'; protected $fillable = ['post_id']; } + +class Role extends Model +{ + use SoftDeletes; + public $table = 'roles'; + protected $guarded = []; +} + +class RoleObserver +{ + public static $model; + + public function forceDeleted($model) + { + static::$model = $model; + } +}