public function match(array $results, $relation)
    {
        $foreign = $this->foreignKey;

        $other = $this->otherKey;

        // Execute the relationship and get related entities as an EntityCollection
        $entities = $this->getEager();

        // 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 = [];

        // TODO ; see if otherKey is the primary key of the related entity, we can
        // simply use the EntityCollection key to match entities to results, which
        // will be much more efficient, and use this method as a fallback if the
        // otherKey is not the same as the primary Key.
        foreach ($entities as $entity) {
            $entity = $this->factory->make($entity);
            $dictionary[$entity->getEntityAttribute($other)] = $entity->getObject();
        }

        // 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.
        return array_map(function ($result) use ($dictionary, $foreign, $relation) {
            if (array_key_exists($foreign, $result) && isset($dictionary[$result[$foreign]])) {
                $result[$relation] = $dictionary[$result[$foreign]];
            } else {
                $result[$relation] = null;
            }

            return $result;
        }, $results);
    }
public function match(array $results, $relation)
    {
        $foreign = $this->foreignKey;

        $other = $this->otherKey;

        // Execute the relationship and get related entities as an EntityCollection
        $entities = $this->getEager();

        // 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 = [];

        // TODO ; see if otherKey is the primary key of the related entity, we can
        // simply use the EntityCollection key to match entities to results, which
        // will be much more efficient, and use this method as a fallback if the
        // otherKey is not the same as the primary Key.
        foreach ($entities as $entity) {
            $entity = $this->factory->make($entity);
            $dictionary[$entity->getEntityAttribute($other)] = $entity->getObject();
        }

        // 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.
        return array_map(function ($result) use ($dictionary, $foreign, $relation) {
            if (array_key_exists($foreign, $result) && isset($dictionary[$result[$foreign]])) {
                $result[$relation] = $dictionary[$result[$foreign]];
            } else {
                $result[$relation] = null;
            }

            return $result;
        }, $results);
    }
public function match ( array $ results , $ relation ) { $ foreign = $ this -> foreignKey ; $ other = $ this -> otherKey ; // Execute the relationship and get related entities as an EntityCollection $ entities = $ this -> getEager ( ) ; // 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 = [ ] ; // TODO ; see if otherKey is the primary key of the related entity, we can // simply use the EntityCollection key to match entities to results, which // will be much more efficient, and use this method as a fallback if the // otherKey is not the same as the primary Key. foreach ( $ entities as $ entity ) { $ entity = $ this -> factory -> make ( $ entity ) ; $ dictionary [ $ entity -> getEntityAttribute ( $ other ) ] = $ entity -> getObject ( ) ; } // 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. return array_map ( function ( $ result ) use ( $ dictionary , $ foreign , $ relation ) { if ( array_key_exists ( $ foreign , $ result ) && isset ( $ dictionary [ $ result [ $ foreign ] ] ) ) { $ result [ $ relation ] = $ dictionary [ $ result [ $ foreign ] ] ; } else { $ result [ $ relation ] = null ; } return $ result ; } , $ results ) ; }
Match the Results array to an eagerly loaded relation.

@param array  $results
@param string $relation

@return array
Match the Results array to an eagerly loaded relation .