From aa169cfb23b673726a14eb55dfebb81258f8b348 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 11 Apr 2022 11:50:12 +0200 Subject: [PATCH 1/2] Fix base query for relationships --- src/Illuminate/Database/Eloquent/Relations/Relation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 58d6b317a6b5..6925447db15e 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -317,7 +317,7 @@ public function getBaseQuery() */ public function toBase() { - return $this->query->getQuery(); + return $this->query->applyScopes()->getQuery(); } /** From 1cbcaa7834ff468f27dbadb6cf4ee023ad0413d4 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 11 Apr 2022 12:19:18 +0200 Subject: [PATCH 2/2] Add test --- ...baseEloquentSoftDeletesIntegrationTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index f1fe25956a53..510315ff24e6 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -123,6 +123,19 @@ public function testSoftDeletesAreNotRetrievedFromBaseQuery() $this->assertCount(1, $query->get()); } + public function testSoftDeletesAreNotRetrievedFromRelationshipBaseQuery() + { + [, $abigail] = $this->createUsers(); + + $abigail->posts()->create(['title' => 'Foo']); + $abigail->posts()->create(['title' => 'Bar'])->delete(); + + $query = $abigail->posts()->toBase(); + + $this->assertInstanceOf(Builder::class, $query); + $this->assertCount(1, $query->get()); + } + public function testSoftDeletesAreNotRetrievedFromBuilderHelpers() { $this->createUsers(); @@ -867,13 +880,17 @@ public function testMorphToNonSoftDeletingModel() /** * Helpers... + * + * @return \Illuminate\Tests\Database\SoftDeletesTestUser[] */ protected function createUsers() { $taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); - SoftDeletesTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']); + $abigail = SoftDeletesTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']); $taylor->delete(); + + return [$taylor, $abigail]; } /**