protected function loadMissingRelation(self $models, array $path)
    {
        $relation = array_shift($path);

        $name = explode(':', key($relation))[0];

        if (is_string(reset($relation))) {
            $relation = reset($relation);
        }

        $models->filter(function ($model) use ($name) {
            return ! is_null($model) && ! $model->relationLoaded($name);
        })->load($relation);

        if (empty($path)) {
            return;
        }

        $models = $models->pluck($name);

        if ($models->first() instanceof BaseCollection) {
            $models = $models->collapse();
        }

        $this->loadMissingRelation(new static($models), $path);
    }
protected function loadMissingRelation(self $models, array $path)
    {
        $relation = array_shift($path);

        $name = explode(':', key($relation))[0];

        if (is_string(reset($relation))) {
            $relation = reset($relation);
        }

        $models->filter(function ($model) use ($name) {
            return ! is_null($model) && ! $model->relationLoaded($name);
        })->load($relation);

        if (empty($path)) {
            return;
        }

        $models = $models->pluck($name);

        if ($models->first() instanceof BaseCollection) {
            $models = $models->collapse();
        }

        $this->loadMissingRelation(new static($models), $path);
    }
protected function loadMissingRelation ( self $ models , array $ path ) { $ relation = array_shift ( $ path ) ; $ name = explode ( ':' , key ( $ relation ) ) [ 0 ] ; if ( is_string ( reset ( $ relation ) ) ) { $ relation = reset ( $ relation ) ; } $ models -> filter ( function ( $ model ) use ( $ name ) { return ! is_null ( $ model ) && ! $ model -> relationLoaded ( $ name ) ; } ) -> load ( $ relation ) ; if ( empty ( $ path ) ) { return ; } $ models = $ models -> pluck ( $ name ) ; if ( $ models -> first ( ) instanceof BaseCollection ) { $ models = $ models -> collapse ( ) ; } $ this -> loadMissingRelation ( new static ( $ models ) , $ path ) ; }
Load a relationship path if it is not already eager loaded.

@param  \Illuminate\Database\Eloquent\Collection  $models
@param  array  $path
@return void
Load a relationship path if it is not already eager loaded .