LMongo\Eloquent\Relations\BelongsTo::match PHP Method

match() public method

Match the eagerly loaded results to their parents.
public match ( array $models, Collection $results, string $relation ) : array
$models array
$results LMongo\Eloquent\Collection
$relation string
return array
    public function match(array $models, Collection $results, $relation)
    {
        $foreign = $this->foreignKey;
        // 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 = array();
        foreach ($results as $result) {
            $dictionary[$result->getKey()] = $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[(string) $model->{$foreign}])) {
                $model->setRelation($relation, $dictionary[(string) $model->{$foreign}]);
            }
        }
        return $models;
    }