Skip to content

Commit 77755e9

Browse files
authored
[9.x] Fix base query for relationships (#41918)
* Fix base query for relationships * Add test
1 parent ae8f205 commit 77755e9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public function getBaseQuery()
317317
*/
318318
public function toBase()
319319
{
320-
return $this->query->getQuery();
320+
return $this->query->applyScopes()->getQuery();
321321
}
322322

323323
/**

tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ public function testSoftDeletesAreNotRetrievedFromBaseQuery()
123123
$this->assertCount(1, $query->get());
124124
}
125125

126+
public function testSoftDeletesAreNotRetrievedFromRelationshipBaseQuery()
127+
{
128+
[, $abigail] = $this->createUsers();
129+
130+
$abigail->posts()->create(['title' => 'Foo']);
131+
$abigail->posts()->create(['title' => 'Bar'])->delete();
132+
133+
$query = $abigail->posts()->toBase();
134+
135+
$this->assertInstanceOf(Builder::class, $query);
136+
$this->assertCount(1, $query->get());
137+
}
138+
126139
public function testSoftDeletesAreNotRetrievedFromBuilderHelpers()
127140
{
128141
$this->createUsers();
@@ -867,13 +880,17 @@ public function testMorphToNonSoftDeletingModel()
867880

868881
/**
869882
* Helpers...
883+
*
884+
* @return \Illuminate\Tests\Database\SoftDeletesTestUser[]
870885
*/
871886
protected function createUsers()
872887
{
873888
$taylor = SoftDeletesTestUser::create(['id' => 1, 'email' => '[email protected]']);
874-
SoftDeletesTestUser::create(['id' => 2, 'email' => '[email protected]']);
889+
$abigail = SoftDeletesTestUser::create(['id' => 2, 'email' => '[email protected]']);
875890

876891
$taylor->delete();
892+
893+
return [$taylor, $abigail];
877894
}
878895

879896
/**

0 commit comments

Comments
 (0)