diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index cac971ccd59b..aa6c81784972 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -64,9 +64,9 @@ public function forceDelete() protected function performDeleteOnModel() { if ($this->forceDeleting) { - $this->exists = false; - - return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(); + return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () { + $this->exists = false; + }); } return $this->runSoftDelete(); diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 9bc91cbbb882..786c9c35242b 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -11,6 +11,7 @@ use Illuminate\Pagination\CursorPaginator; use Illuminate\Pagination\Paginator; use Illuminate\Support\Carbon; +use Mockery; use PHPUnit\Framework\TestCase; class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase @@ -189,6 +190,42 @@ public function testForceDeleteActuallyDeletesRecords() $this->assertEquals(1, $users->first()->id); } + public function testForceDeleteUpdateExistsProperty() + { + $this->createUsers(); + $user = SoftDeletesTestUser::find(2); + + $this->assertTrue($user->exists); + + $user->forceDelete(); + + $this->assertFalse($user->exists); + } + + public function testForceDeleteDoesntUpdateExistsPropertyIfFailed() + { + $user = new class() extends SoftDeletesTestUser + { + public $exists = true; + + public function newModelQuery() + { + return Mockery::spy(parent::newModelQuery(), function (Mockery\MockInterface $mock) { + $mock->shouldReceive('forceDelete')->andThrow(new \Exception()); + }); + } + }; + + $this->assertTrue($user->exists); + + try { + $user->forceDelete(); + } catch (\Exception $exception) { + } + + $this->assertTrue($user->exists); + } + public function testRestoreRestoresRecords() { $this->createUsers();