8000 [12.x] Ensure related models is iterable on `HasRelationships@relatio… · laravel/framework@63b4d72 · GitHub
[go: up one dir, main page]

Skip to content

Commit 63b4d72

Browse files
authored
[12.x] Ensure related models is iterable on HasRelationships@relationLoaded() (#55519)
* ensure related models is iterable * add test case
1 parent c65ac53 commit 63b4d72

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,11 @@ public function relationLoaded($key)
10861086
}
10871087

10881088
if ($nestedRelation !== null) {
1089-
foreach ($this->$relation as $related) {
1089+
$relatedModels = is_iterable($relatedModels = $this->$relation)
1090+
? $relatedModels
1091+
: [$relatedModels];
1092+
1093+
foreach ($relatedModels as $related) {
10901094
if (! $related->relationLoaded($nestedRelation)) {
10911095
return false;
10921096
}

tests/Integration/Database/EloquentModelRelationLoadedTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ public function testWhenChildRecursiveRelationIsLoaded()
119119
$this->assertTrue($model->relationLoaded('twos.threes'));
120120
$this->assertTrue($model->relationLoaded('twos.threes.one'));
121121
}
122+
123+
public function testWhenParentRelationIsASingleInstance()
124+
{
125+
$one = One::query()->create();
126+
$two = $one->twos()->create();
127+
$three = $two->threes()->create();
128+
129+
$model = Three::query()
130+
->with('two.one')
131+
->find($three->id);
132+
133+
$this->assertTrue($model->relationLoaded('two'));
134+
$this->assertTrue($model->two->is($two));
135+
$this->assertTrue($model->relationLoaded('two.one'));
136+
$this->assertTrue($model->two->one->is($one));
137+
}
122138
}
123139

124140
class One extends Model

0 commit comments

Comments
 (0)
0