Skip to content

Conversation

@druc
Copy link

@druc druc commented Nov 1, 2017

Hello,

Whenever I try to eager load a hasManyThrough relation that uses a custom intermediate and a local key I get an empty collection.

This happens because when the match(array $models, Collection $results, $relation) on the HasManyThrough class is called, the relation is set using the $model->getKey rather than my provided $this->localKey attribute.

Changing

foreach ($models as $model) {
    if (isset($dictionary[$key = $model->getKey()])) {
        //
    }
}

into

foreach ($models as $model) {
    if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) {
        //
    }
}

fixes it because $this->localKey defaults to the pk anyway.

Also, I think this is also a fix for this issue #15909 (haven't tested, but looks like the same problem)

@druc druc force-pushed the fix_eager_loading_relation_with_custom_intermediate_and_local_key branch from 5fb98aa to 505dbf8 Compare November 1, 2017 00:33
@taylorotwell taylorotwell merged commit d455b40 into laravel:5.5 Nov 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants