diff --git a/src/Database/Model.php b/src/Database/Model.php index 23b36ec6..f88bab6e 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -1017,7 +1017,7 @@ protected function performDeleteOnRelations() continue; } // we want to remove the pivot record, not the actual relation record - $relation()->detach(); + $this->{$name}()->detach(); } else { if (!Arr::get($options, 'delete', false)) { continue; diff --git a/src/Database/Traits/SoftDelete.php b/src/Database/Traits/SoftDelete.php index c655b27e..aedeec81 100644 --- a/src/Database/Traits/SoftDelete.php +++ b/src/Database/Traits/SoftDelete.php @@ -127,6 +127,7 @@ protected function performSoftDeleteOnRelations() // relations using pivot table $value = $this->fromDateTime($this->freshTimestamp()); $this->updatePivotDeletedAtColumn($name, $options, $value); + } else { if ($relation instanceof EloquentModel) { $relation->delete(); @@ -187,12 +188,10 @@ public function restore() */ protected function updatePivotDeletedAtColumn(string $relationName, array $options, string|null $value) { - $relation = $this->{$relationName}; - // get deletedAtColumn from the relation options, otherwise use default $deletedAtColumn = array_get($options, 'deletedAtColumn', 'deleted_at'); - $query = $relation()->newPivotQuery()->update([ + $this->{$relationName}()->newPivotQuery()->update([ $deletedAtColumn => $value, ]); } diff --git a/tests/Database/Traits/SoftDeleteTest.php b/tests/Database/Traits/SoftDeleteTest.php index fba13d0c..258b8208 100644 --- a/tests/Database/Traits/SoftDeleteTest.php +++ b/tests/Database/Traits/SoftDeleteTest.php @@ -16,6 +16,7 @@ public function setUp(): void ]; $this->createTables(); + $this->seedTables(); } protected function createTables() @@ -39,8 +40,6 @@ protected function createTables() $table->unsignedInteger('category_id'); $table->timestamp('deleted_at')->nullable(); }); - - $this->seedTables(); } protected function seedTables() @@ -68,7 +67,7 @@ protected function seedTables() public function testDeleteAndRestore() { - $post = Post::where('title', 'First Post')->first(); + $post = Post::first(); $this->assertTrue($post->deleted_at === null); $this->assertTrue($post->categories()->where('deleted_at', null)->count() === 2); @@ -102,9 +101,9 @@ class Post extends \Winter\Storm\Database\Model public $belongsToMany = [ 'categories' => [ Category::class, - 'table' => 'categories_posts', - 'key' => 'post_id', - 'otherKey' => 'category_id', + 'table' => 'categories_posts', + 'key' => 'post_id', + 'otherKey' => 'category_id', 'softDelete' => true, ], ]; @@ -124,9 +123,9 @@ class Category extends \Winter\Storm\Database\Model public $belongsToMany = [ 'posts' => [ Post::class, - 'table' => 'categories_posts', - 'key' => 'category_id', - 'otherKey' => 'post_id', + 'table' => 'categories_posts', + 'key' => 'category_id', + 'otherKey' => 'post_id', ], ]; }