public function hasOneThroughSeveral(array $relations, $foreignKey = null, $localKey = null)
    {
        $helpers = new Helpers($this);
        $currentRelation = current($relations);
        $related = $helpers->getClassNameFromCurrent($currentRelation, $relations);
        $firstRelation = new $related();

        $instance = $this->newRelatedInstance($related);
        $query = $instance
            ->newQuery()
            ->select($firstRelation->getTable().'.*');

        $joins = $helpers->getJoinsRelations($relations, $localKey);
        foreach ($joins as $join) {
            $query->join($join['table'], $join['key'], '=', $join['foreign_key']);
            // $query->addSelect($join['select_key']);
            // $query->addSelect($join['select_key'].' as has_one_'.$join['select_key']);
            // $query->addSelect($join['select_key'].' as has_one_'.str_replace('.', '_', $join['select_key']));
        }

        $key = $localKey ?? $this->getKeyName();

        $foreignKey = $foreignKey ?? $this->getTable().'.'.$firstRelation->getKeyName();

        $localKey = $localKey ?? $firstRelation->getKeyName();

        return new HasOneThroughSeveral($query, $this, $foreignKey, $localKey);
    }
public function hasOneThroughSeveral(array $relations, $foreignKey = null, $localKey = null)
    {
        $helpers = new Helpers($this);
        $currentRelation = current($relations);
        $related = $helpers->getClassNameFromCurrent($currentRelation, $relations);
        $firstRelation = new $related();

        $instance = $this->newRelatedInstance($related);
        $query = $instance
            ->newQuery()
            ->select($firstRelation->getTable().'.*');

        $joins = $helpers->getJoinsRelations($relations, $localKey);
        foreach ($joins as $join) {
            $query->join($join['table'], $join['key'], '=', $join['foreign_key']);
            // $query->addSelect($join['select_key']);
            // $query->addSelect($join['select_key'].' as has_one_'.$join['select_key']);
            // $query->addSelect($join['select_key'].' as has_one_'.str_replace('.', '_', $join['select_key']));
        }

        $key = $localKey ?? $this->getKeyName();

        $foreignKey = $foreignKey ?? $this->getTable().'.'.$firstRelation->getKeyName();

        $localKey = $localKey ?? $firstRelation->getKeyName();

        return new HasOneThroughSeveral($query, $this, $foreignKey, $localKey);
    }
public function hasOneThroughSeveral ( array $ relations , $ foreignKey = null , $ localKey = null ) { $ helpers = new Helpers ( $ this ) ; $ currentRelation = current ( $ relations ) ; $ related = $ helpers -> getClassNameFromCurrent ( $ currentRelation , $ relations ) ; $ firstRelation = new $ related ( ) ; $ instance = $ this -> newRelatedInstance ( $ related ) ; $ query = $ instance -> newQuery ( ) -> select ( $ firstRelation -> getTable ( ) . '.*' ) ; $ joins = $ helpers -> getJoinsRelations ( $ relations , $ localKey ) ; foreach ( $ joins as $ join ) { $ query -> join ( $ join [ 'table' ] , $ join [ 'key' ] , '=' , $ join [ 'foreign_key' ] ) ; // $query->addSelect($join['select_key']); // $query->addSelect($join['select_key'].' as has_one_'.$join['select_key']); // $query->addSelect($join['select_key'].' as has_one_'.str_replace('.', '_', $join['select_key'])); } $ key = $ localKey ?? $ this -> getKeyName ( ) ; $ foreignKey = $ foreignKey ?? $ this -> getTable ( ) . '.' . $ firstRelation -> getKeyName ( ) ; $ localKey = $ localKey ?? $ firstRelation -> getKeyName ( ) ; return new HasOneThroughSeveral ( $ query , $ this , $ foreignKey , $ localKey ) ; }
Define a one-to-one-through-several relationship.

@param array  $relations
@param string $foreignKey
@param string $localKey

@return \Illuminate\Database\Eloquent\Relations\HasOne
Define a one - to - one - through - several relationship .