Load all the models for the provided relations. This is the meat of the eager loading

@param  Models           $models
@param  array            $rels
@param  int              $flags
@return Repo $this
979719
public function loadAllRelsFor(Models $models, array $rels, $flags = null)
    {
        $rels = Arr::toAssoc($rels);

        foreach ($rels as $relName => $childRels) {
            $foreign = $this->loadRelFor($models, $relName, $flags);

            if ($childRels) {
                $rel = $this->getRel($relName);
                $rel->getRepo()->loadAllRelsFor($foreign, $childRels, $flags);
            }
        }

        return $this;
    }
615929
public function addEagerConstraints(array $models)
    {
        $this->query->whereIn(
            $this->getQualifiedFirstKeyName(), $this->getKeys($models, $this->localKey)
        );
    }
1105100
public function syncRoles($slugs, $reload = true)
    {
        /** @var \Illuminate\Database\Eloquent\Collection $roles */
        $roles = app(RoleContract::class)->whereIn('slug', $slugs)->get();

        event(new SyncingRolesWithPermission($this, $roles));
        $synced = $this->roles()->sync($roles->pluck('id'));
        event(new SyncedRolesWithPermission($this, $roles, $synced));

        $this->loadRoles($reload);

        return $synced;
    }
391835
public function setRelatedModels($models)
    {
        $models = $this->parseAsArray($models);
        foreach ($models as $i => $model) {
            if (is_string($model)) {
                list($model, $prop) = $this->resolveRelatedModel($model);
                $models[$i]                                 = $model;
                $this->relatedProperties[$model->objType()] = $prop;
            } elseif ($model instanceof ModelInterface) {
                if (!isset($this->relatedProperties[$model->objType()])) {
                    throw new InvalidArgumentException(
                        sprintf(
                            'The related model [%s] requires a target property',
                            get_class($model)
                        )
                    );
                }
                $models[$i] = $model;
            } else {
                throw new InvalidArgumentException(
                    sprintf(
                        'A related model must be defined as "%s"',
                        'ObjType:propertyIdent'
                    )
                );
            }
        }

        $this->relatedModels = $models;

        return $this;
    }
362218
public function addEagerConstraints(array $models)
    {
        $whereIn = $this->whereInMethod($this->parent, $this->localKey);

        $this->query->{$whereIn}(
            $this->foreignKey, $this->getKeys($models, $this->localKey)
        );
    }
1285173
protected function eagerLoadRelation(array $models, $name, Closure $constraints)
    {
        // First we will "back up" the existing where conditions on the query so we can
        // add our eager constraints. Then we will merge the wheres that were on the
        // query back to it in order that any where conditions might be specified.
        $relation = $this->getRelation($name);

        $relation->addEagerConstraints($models);

        $constraints($relation);

        // Once we have the results, we just match those back up to their parent models
        // using the relationship instance. Then we just return the finished arrays
        // of models which have been eagerly hydrated and are readied for return.
        return $relation->match(
            $relation->initRelation($models, $name),
            $relation->getEager(), $name
        );
    }
298691
public function setManyToManyRelation($rel)
    {
        $key = $rel['refTable']->getModelName();
        $this->_m2mRelations[$key] = $rel;
        $this->getDocument()->addLog(sprintf('Applying N <=> N relation "%s" for "%s <=> %s"', $rel['refTable']->getParameters()->get('name'), $this->getModelName(), $key));

        return $this;
    }
345160
public function reload($resetRelations = false) {   // TODO: implement and set default=TRUE
        if (!$this->isPersistent()) throw new IllegalStateException('Cannot reload non-persistent '.get_class($this));

        // TODO: This method cannot yet handle composite primary keys.

        $db     = $this->db();
        $dao    = $this->dao();
        $entity = $dao->getEntityMapping();
        $table  = $entity->getTableName();

        // collect identity infos
        $identity = $entity->getIdentity();
        $idColumn = $identity->getColumn();
        $idName   = $identity->getName();
        $idValue  = $identity->convertToDBValue($this->$idName, $db);

        // create and execute SQL
        $sql = 'select *
                   from '.$table.'
                   where '.$idColumn.' = '.$idValue;
        $row = $db->query($sql)->fetchRow();
        if ($row === null) throw new ConcurrentModificationException('Error reloading '.get_class($this).' ('.$this->getObjectId().'), record not found');

        // apply record values
        return $this->populate($row);
    }
377401
public function loadCount($relations)
    {
        if ($this->isEmpty()) {
            return $this;
        }

        $models = $this->first()->newModelQuery()
            ->whereKey($this->modelKeys())
            ->select($this->first()->getKeyName())
            ->withCount(...func_get_args())
            ->get();

        $attributes = Arr::except(
            array_keys($models->first()->getAttributes()),
            $models->first()->getKeyName()
        );

        $models->each(function ($model) use ($attributes) {
            $this->find($model->getKey())->forceFill(
                Arr::only($model->getAttributes(), $attributes)
            )->syncOriginalAttributes($attributes);
        });

        return $this;
    }
576556
public function addEagerConstraints(array $models)
    {
        $keys = $this->getKeys($models, $this->getFirstForeignKeyName());

        $this->query->whereIn($this->getQualifiedFirstLocalKeyName(), $keys);
    }
684938
public function loadMissing($relations)
    {
        $relations = is_string($relations) ? func_get_args() : $relations;

        $this->newCollection([$this])->loadMissing($relations);

        return $this;
    }
160794
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->getDefaultFor($model));
        }

        return $models;
    }
926862
public function load($relations)
    {
        $query = $this->newQuery()->with(
            is_string($relations) ? func_get_args() : $relations
        );

        $query->eagerLoadRelations([$this]);

        return $this;
    }
231195
public function build(array $results, array $eagerLoads)
    {
        // First, we'll cache the raw result set
        $this->cacheResults($results);

        // Parse embedded relations and build corresponding entities using the default
        // mapper.
        $results = $this->buildEmbeddedRelationships($results);

        // Launch the queries related to eager loads, and match the
        // current result set to these loaded relationships.
        $results = $this->queryEagerLoadedRelationships($results, $eagerLoads);

        return $this->buildResultSet($results);
    }
702424
protected function getQueryResults(array $models) {
        if (empty($this->relations)) {
            return static::getRelationQuery($models)->all();
        }
        $data = static::getRelationQuery($models)->appendSelect($this->getRelationFields())->all();
        if (empty($data)) {
            return $data;
        }
        foreach ($this->relations as $relation) {
            if (empty($relation->getKey())) {
                return $relation->getResults($data);
            }
            $data = $relation->getResults($data);
        }
        return $data;
    }
1095554
public function loadRelFor(Models $models, $relName, $flags = null)
    {
        $rel = $this->getRelOrError($relName);

        $foreign = $rel->loadModelsIfAvailable($models, $flags);

        $rel->linkModels($models, $foreign, function (AbstractLink $link) {
            $class = get_class($link->getModel());
            $class::getRepo()->addLink($link);
        });

        return $foreign;
    }
1366572
public function parentRelations(array $relations): ModelList
    {
        $this->main['parents'] = [];

        foreach ($relations as $key => $value) {

            if (is_string($key)) {
                $parent = [
                    'relation' => $key,
                    'field'    => $value,
                ];
            } else {
                $parent = [
                    'relation' => $value,
                    'field'    => $value,
                ];
            }

            $this->main['parents'][] = $parent;
        }

        return $this;
    }