Skip to content

Commit deed7dd

Browse files
committed
Already deleted items should not be in the affectedRows
1 parent c5dec6c commit deed7dd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/SoftCascade.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ protected function execute($relation, $foreignKey, $foreignKeyIds, $affectedRows
199199
$relationModel = $relation->getQuery()->getModel();
200200
$relationModel = new $relationModel();
201201
if ($affectedRows > 0) {
202-
$relationModel = $relationModel->withTrashed()->whereIn($foreignKey, $foreignKeyIds)->limit($affectedRows);
202+
if ($this->direction != 'delete') {
203+
$relationModel = $relationModel->withTrashed();
204+
}
205+
206+
$relationModel = $relationModel->whereIn($foreignKey, $foreignKeyIds)->limit($affectedRows);
207+
203208
$this->run($relationModel->get([$relationModel->getModel()->getKeyName()]));
204209
$relationModel->{$this->direction}($this->directionData);
205210
}
@@ -253,7 +258,11 @@ protected function affectedRows($relation, $foreignKey, $foreignKeyIds)
253258
$relationModel = $relation->getQuery()->getModel();
254259
$relationModel = new $relationModel();
255260

256-
return $relationModel->withTrashed()->whereIn($foreignKey, $foreignKeyIds)->count();
261+
if ($this->direction != 'delete') {
262+
$relationModel = $relationModel->withTrashed();
263+
}
264+
265+
return $relationModel->whereIn($foreignKey, $foreignKeyIds)->count();
257266
}
258267

259268
/**

tests/IntegrationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,23 @@ public function testMultipleDelete()
248248
$this->assertEquals(1, Profiles::count());
249249
}
250250

251+
public function testKeepDeletedDates()
252+
{
253+
$this->createPostAndCategoriesRaw();
254+
$post = Post::first();
255+
$post->deleted_at = '2011-01-01';
256+
$post->save();
257+
$this->assertSoftDeleted('posts', ['id' => $post->id]);
258+
$categoryToDelete = Category::with('posts')->first();
259+
$categoryToDelete->delete();
260+
$this->assertSoftDeleted('categories', ['id' => $categoryToDelete->id]);
261+
$categoryToDelete->posts->each(function ($post) {
262+
$this->assertSoftDeleted('posts', ['id' => $post->id]);
263+
});
264+
$posts = Post::withTrashed()->get();
265+
$this->assertNotEquals($posts->first()->deleted_at, $posts->last()->deleted_at);
266+
}
267+
251268
public function testMultipleRestore()
252269
{
253270
$this->createUserRaw();

0 commit comments

Comments
 (0)