Skip to content
Closed
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
14 changes: 11 additions & 3 deletions src/Model/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,23 @@ public function match(array $models, Collection $results, $relation)
$dictionary = [];

foreach ($results as $result) {
$dictionary[$result->getAttribute($owner)] = $result;
// The owner key can be null when not selected; null as an array offset is deprecated since PHP 8.1
$key = $result->getAttribute($owner);

if ($key !== null) {
$dictionary[$key] = $result;
}
}

// Once we have the dictionary constructed, we can loop through all the parents
// and match back onto their children using these keys of the dictionary and
// the primary key of the children to map them onto the correct instances.
foreach ($models as $model) {
if (isset($dictionary[$model->{$foreign}])) {
$model->setRelation($relation, $dictionary[$model->{$foreign}]);
// The foreign key is nullable, and using null as an array offset is deprecated since PHP 8.1
$key = $model->{$foreign};

if ($key !== null && isset($dictionary[$key])) {
$model->setRelation($relation, $dictionary[$key]);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Model/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ public function morphWith(array $with)
protected function buildDictionary(Collection $models)
{
foreach ($models as $model) {
if ($model->{$this->morphType}) {
$this->dictionary[$model->{$this->morphType}][$model->{$this->foreignKey}][] = $model;
// Both are nullable, and using null as an array offset is deprecated since PHP 8.1
$type = $model->{$this->morphType};
$key = $model->{$this->foreignKey};

if (empty($type) || $key === null) {
continue;
}

$this->dictionary[$type][$key][] = $model;
}
}

Expand Down
Loading