src/Relations/HasOneOrMany.php
protected function fetch_results_for_eager_load( array $models, $callback = null ) {

		$cached = array();

		if ( $callback === null ) {
			foreach ( $models as $pk => $model ) {
				$result = $this->load_from_cache( $model );

				if ( $result ) {
					$cached[ $pk ] = $result instanceof Collection ? $result->toArray() : array( $result );
				}
			}

			if ( count( $cached ) === count( $models ) ) {
				return new Collection( $this->flatten( $cached ), false, $this->saver );
			}
		}

		/** @var FluentQuery $query */
		$query = call_user_func( array( $this->related_model, 'query_with_no_global_scopes' ) );

		$pks = array_keys( $models );
		$pks = array_diff( $pks, array_keys( $cached ) );
		$pks = array_filter( $pks );

		if ( count( $pks ) === 0 ) {
			return new Collection( array(), false, $this->saver );
		}

		$query->where( $this->foreign_key, true, $pks );

		$this->apply_scopes_for_eager_load( $query );

		if ( $callback ) {
			$callback( $query );
		}

		$results = $query->results()->toArray();
		$cached  = $this->flatten( $cached );

		return new Collection( array_merge( $results, $cached ), false, $this->saver );
	}
protected function fetch_results_for_eager_load( array $models, $callback = null ) {

		$cached = array();

		if ( $callback === null ) {
			foreach ( $models as $pk => $model ) {
				$result = $this->load_from_cache( $model );

				if ( $result ) {
					$cached[ $pk ] = $result instanceof Collection ? $result->toArray() : array( $result );
				}
			}

			if ( count( $cached ) === count( $models ) ) {
				return new Collection( $this->flatten( $cached ), false, $this->saver );
			}
		}

		/** @var FluentQuery $query */
		$query = call_user_func( array( $this->related_model, 'query_with_no_global_scopes' ) );

		$pks = array_keys( $models );
		$pks = array_diff( $pks, array_keys( $cached ) );
		$pks = array_filter( $pks );

		if ( count( $pks ) === 0 ) {
			return new Collection( array(), false, $this->saver );
		}

		$query->where( $this->foreign_key, true, $pks );

		$this->apply_scopes_for_eager_load( $query );

		if ( $callback ) {
			$callback( $query );
		}

		$results = $query->results()->toArray();
		$cached  = $this->flatten( $cached );

		return new Collection( array_merge( $results, $cached ), false, $this->saver );
	}
Fetch results for eager loading.

@since 2.0

@param Model[]  $models
@param callable $callback

@return Collection