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

[8.x] Eager loading MorphTo relationship does not honor each models $keyType #34531

Merged
merged 3 commits into from
Sep 24, 2020
Merged
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
11 changes: 9 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,25 @@ protected function getResultsByType($type)
$whereIn = $this->whereInMethod($instance, $ownerKey);

return $query->{$whereIn}(
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type)
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type, $instance->getKeyType())
)->get();
}

/**
* Gather all of the foreign keys for a given type.
*
* @param string $type
* @param string $keyType
* @return array
*/
protected function gatherKeysByType($type)
protected function gatherKeysByType($type, $keyType)
{
if ($keyType === 'string') {
return array_map(function ($modelId) {
return (string) $modelId;
}, array_keys($this->dictionary[$type]));
}

return array_keys($this->dictionary[$type]);
}

Expand Down