Skip to content

Commit 0bb094b

Browse files
committed
Slight tweak/document Eloquent relationship ID loading
1 parent ff9462f commit 0bb094b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/Adapter/EloquentAdapter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,19 @@ public function loadIds(array $models, Relationship $relationship): void
202202
$property = $this->getRelationshipProperty($relationship);
203203
$relation = $models[0]->$property();
204204

205-
if ($relation instanceof BelongsTo || $relation instanceof BelongsToMany) {
205+
// If it's a belongs-to relationship, then the ID is stored on the model
206+
// itself, so we don't need to load anything in advance.
207+
if ($relation instanceof BelongsTo) {
206208
return;
207209
}
208210

209211
(new Collection($models))->loadMissing([
210212
$property => function ($query) use ($relation) {
211-
$query->select([
212-
$relation->getRelated()->getKeyName(),
213-
$relation->getForeignKeyName()
214-
]);
213+
$query->select($relation->getRelated()->getKeyName());
214+
215+
if (! $relation instanceof BelongsToMany) {
216+
$query->addSelect($relation->getForeignKeyName());
217+
}
215218
}
216219
]);
217220
}

0 commit comments

Comments
 (0)