public function sortByRelation($column, $direction = 'ASC')
    {
        $relations = explode('.', $column);
        $column    = array_pop($relations);

        /** @var \Illuminate\Database\Eloquent\Model $model */
        $model = $this->repository->makeModel();

        $this->instance->getQuery()->orders = [];

        foreach ($relations as $relation) {
            /* @var Relation $relationClass */
            $this->joinRelation($relationClass = $model->{camel_case($relation)}());

            $model = $relationClass->getRelated();
        }

        $selectedColumns = $this->instance->getQuery()->columns;

        $addColumn = DB::raw("{$this->repository->makeModel()->getTable()}.*");

        if (is_array($selectedColumns) && array_search('*', $selectedColumns, true) !== null) {
            $this->instance->getQuery()->columns[array_search('*', $selectedColumns, true)] = $addColumn;
        } else {
            $this->instance->select($addColumn);
        }

        $this->instance->orderBy("{$model->getTable()}.{$column}", $direction);

        return $this;
    }
public function sortByRelation($column, $direction = 'ASC')
    {
        $relations = explode('.', $column);
        $column    = array_pop($relations);

        /** @var \Illuminate\Database\Eloquent\Model $model */
        $model = $this->repository->makeModel();

        $this->instance->getQuery()->orders = [];

        foreach ($relations as $relation) {
            /* @var Relation $relationClass */
            $this->joinRelation($relationClass = $model->{camel_case($relation)}());

            $model = $relationClass->getRelated();
        }

        $selectedColumns = $this->instance->getQuery()->columns;

        $addColumn = DB::raw("{$this->repository->makeModel()->getTable()}.*");

        if (is_array($selectedColumns) && array_search('*', $selectedColumns, true) !== null) {
            $this->instance->getQuery()->columns[array_search('*', $selectedColumns, true)] = $addColumn;
        } else {
            $this->instance->select($addColumn);
        }

        $this->instance->orderBy("{$model->getTable()}.{$column}", $direction);

        return $this;
    }
public function sortByRelation ( $ column , $ direction = 'ASC' ) { $ relations = explode ( '.' , $ column ) ; $ column = array_pop ( $ relations ) ; /** @var \Illuminate\Database\Eloquent\Model $model */ $ model = $ this -> repository -> makeModel ( ) ; $ this -> instance -> getQuery ( ) -> orders = [ ] ; foreach ( $ relations as $ relation ) { /* @var Relation $relationClass */ $ this -> joinRelation ( $ relationClass = $ model -> { camel_case ( $ relation ) } ( ) ) ; $ model = $ relationClass -> getRelated ( ) ; } $ selectedColumns = $ this -> instance -> getQuery ( ) -> columns ; $ addColumn = DB :: raw ( "{$this->repository->makeModel()->getTable()}.*" ) ; if ( is_array ( $ selectedColumns ) && array_search ( '*' , $ selectedColumns , true ) !== null ) { $ this -> instance -> getQuery ( ) -> columns [ array_search ( '*' , $ selectedColumns , true ) ] = $ addColumn ; } else { $ this -> instance -> select ( $ addColumn ) ; } $ this -> instance -> orderBy ( "{$model->getTable()}.{$column}" , $ direction ) ; return $ this ; }
Append relation column sorting to query builder.

@param array  $column
@param string $direction

@return $this
Append relation column sorting to query builder .