src/Execution/DataLoader/ModelRelationFetcher.php
protected function buildRelationsFromModels(string $relationName, Closure $relationConstraints): Collection
    {
        return $this->models->toBase()->map(
            function (Model $model) use ($relationName, $relationConstraints) {
                $relation = $this->getRelationInstance($relationName);

                $relation->addEagerConstraints([$model]);

                // Call the constraints
                $relationConstraints($relation, $model);

                if (method_exists($relation, 'shouldSelect')) {
                    $shouldSelect = new ReflectionMethod(get_class($relation), 'shouldSelect');
                    $shouldSelect->setAccessible(true);
                    $select = $shouldSelect->invoke($relation, ['*']);

                    $relation->addSelect($select);
                } elseif (method_exists($relation, 'getSelectColumns')) {
                    $getSelectColumns = new ReflectionMethod(get_class($relation), 'getSelectColumns');
                    $getSelectColumns->setAccessible(true);
                    $select = $getSelectColumns->invoke($relation, ['*']);

                    $relation->addSelect($select);
                }

                $relation->initRelation([$model], $relationName);

                return $relation;
            }
        );
    }
protected function buildRelationsFromModels(string $relationName, Closure $relationConstraints): Collection
    {
        return $this->models->toBase()->map(
            function (Model $model) use ($relationName, $relationConstraints) {
                $relation = $this->getRelationInstance($relationName);

                $relation->addEagerConstraints([$model]);

                // Call the constraints
                $relationConstraints($relation, $model);

                if (method_exists($relation, 'shouldSelect')) {
                    $shouldSelect = new ReflectionMethod(get_class($relation), 'shouldSelect');
                    $shouldSelect->setAccessible(true);
                    $select = $shouldSelect->invoke($relation, ['*']);

                    $relation->addSelect($select);
                } elseif (method_exists($relation, 'getSelectColumns')) {
                    $getSelectColumns = new ReflectionMethod(get_class($relation), 'getSelectColumns');
                    $getSelectColumns->setAccessible(true);
                    $select = $getSelectColumns->invoke($relation, ['*']);

                    $relation->addSelect($select);
                }

                $relation->initRelation([$model], $relationName);

                return $relation;
            }
        );
    }
protected function buildRelationsFromModels ( string $ relationName , Closure $ relationConstraints ) : Collection { return $ this -> models -> toBase ( ) -> map ( function ( Model $ model ) use ( $ relationName , $ relationConstraints ) { $ relation = $ this -> getRelationInstance ( $ relationName ) ; $ relation -> addEagerConstraints ( [ $ model ] ) ; // Call the constraints $ relationConstraints ( $ relation , $ model ) ; if ( method_exists ( $ relation , 'shouldSelect' ) ) { $ shouldSelect = new ReflectionMethod ( get_class ( $ relation ) , 'shouldSelect' ) ; $ shouldSelect -> setAccessible ( true ) ; $ select = $ shouldSelect -> invoke ( $ relation , [ '*' ] ) ; $ relation -> addSelect ( $ select ) ; } elseif ( method_exists ( $ relation , 'getSelectColumns' ) ) { $ getSelectColumns = new ReflectionMethod ( get_class ( $ relation ) , 'getSelectColumns' ) ; $ getSelectColumns -> setAccessible ( true ) ; $ select = $ getSelectColumns -> invoke ( $ relation , [ '*' ] ) ; $ relation -> addSelect ( $ select ) ; } $ relation -> initRelation ( [ $ model ] , $ relationName ) ; return $ relation ; } ) ; }
Get queries to fetch relationships.

@param  string  $relationName
@param  \Closure  $relationConstraints
@return \Illuminate\Support\Collection<\Illuminate\Database\Eloquent\Relations\Relation>
Get queries to fetch relationships .