/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param Collection $results
* @param string $relation
*
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
$foreign = $this->foreignKey;
$other = $this->otherKey;
// First we will get to build a dictionary of the child models by their primary
// key of the relationship, then we can easily match the children back onto
// the parents using that dictionary and the primary key of the children.
$dictionary = [];
foreach ($results as $result) {
$dictionary[$result->getAttribute($other)] = $result;
}
$fallbackModels = [];
// 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}]);
} else {
$fallbackModels[] = $model;
}
}
if (count($fallbackModels) > 0) {
$query = $this->query;
$lang = $this->lang;
$this->lang = $this->fallback;
$this->query = $this->fallbackQuery;
$this->addEagerConstraints($fallbackModels);
parent::match($fallbackModels, $this->getEager(), $relation);
$this->query = $query;
$this->lang = $lang;
}
return $models;
}