Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Bugfix for calling pluck() on chaperoned relations. #52680

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function applyInverseRelationToCollection($models, ?Model $parent = nu
$parent ??= $this->getParent();

foreach ($models as $model) {
$this->applyInverseRelationToModel($model, $parent);
$model instanceof Model && $this->applyInverseRelationToModel($model, $parent);
}

return $models;
Expand Down
26 changes: 26 additions & 0 deletions tests/Database/DatabaseEloquentInverseRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,27 @@ public function testSetsGuessedInverseRelationBasedOnForeignKey()
$this->assertSame('test', $relation->getInverseRelationship());
}

public function testOnlyHydratesInverseRelationOnModels()
{
$relation = m::mock(HasInverseRelationStub::class)->shouldAllowMockingProtectedMethods()->makePartial();
$relation->shouldReceive('getParent')->andReturn(new HasInverseRelationParentStub);
$relation->shouldReceive('applyInverseRelationToModel')->times(6);
$relation->exposeApplyInverseRelationToCollection([
new HasInverseRelationRelatedStub(),
12345,
new HasInverseRelationRelatedStub(),
new HasInverseRelationRelatedStub(),
Model::class,
new HasInverseRelationRelatedStub(),
true,
[],
new HasInverseRelationRelatedStub(),
'foo',
new class() {},
new HasInverseRelationRelatedStub(),
]);
}

public static function guessedParentRelationsDataProvider()
{
yield ['hasInverseRelationParentStub'];
Expand Down Expand Up @@ -361,4 +382,9 @@ public function exposeGuessInverseRelation(): string|null
{
return $this->guessInverseRelation();
}

public function exposeApplyInverseRelationToCollection($models, ?Model $parent = null)
{
return $this->applyInverseRelationToCollection($models, $parent);
}
}
Loading