public function modelJoin($relationships, $operator = '=', $type = 'left', $where = false)
    {
        if (!is_array($relationships)) {
            $relationships = [$relationships];
        }

        if (empty($this->query->columns)) {
            $this->query->selectRaw('DISTINCT '.$this->model->getTable().'.*');
        }

        foreach ($relationships as $relation_name => $load_relationship) {

            // Required variables.
            $model = Arr::get($this->relationships, $relation_name.'.model');
            $method = Arr::get($this->relationships, $relation_name.'.method');
            $table = Arr::get($this->relationships, $relation_name.'.table');
            $parent_key = Arr::get($this->relationships, $relation_name.'.parent_key');
            $foreign_key = Arr::get($this->relationships, $relation_name.'.foreign_key');

            // Add the columns from the other table.
            // @todo do we need this?
            //$this->query->addSelect(new Expression("`$table`.*"));
            $this->query->join($table, $parent_key, $operator, $foreign_key, $type, $where);

            // The join above is to the intimidatory table. This joins the query to the actual model.
            if ($method === 'BelongsToMany') {
                $related_foreign_key = $model->getQualifiedRelatedKeyName();
                $related_relation = $model->getRelated();
                $related_table = $related_relation->getTable();
                $related_qualified_key_name = $related_relation->getQualifiedKeyName();
                $this->query->join($related_table, $related_qualified_key_name, $operator, $related_foreign_key, $type, $where);
            }
        }

        // Group by the original model.
        $this->query->groupBy($this->model->getQualifiedKeyName());
    }
public function modelJoin($relationships, $operator = '=', $type = 'left', $where = false)
    {
        if (!is_array($relationships)) {
            $relationships = [$relationships];
        }

        if (empty($this->query->columns)) {
            $this->query->selectRaw('DISTINCT '.$this->model->getTable().'.*');
        }

        foreach ($relationships as $relation_name => $load_relationship) {

            // Required variables.
            $model = Arr::get($this->relationships, $relation_name.'.model');
            $method = Arr::get($this->relationships, $relation_name.'.method');
            $table = Arr::get($this->relationships, $relation_name.'.table');
            $parent_key = Arr::get($this->relationships, $relation_name.'.parent_key');
            $foreign_key = Arr::get($this->relationships, $relation_name.'.foreign_key');

            // Add the columns from the other table.
            // @todo do we need this?
            //$this->query->addSelect(new Expression("`$table`.*"));
            $this->query->join($table, $parent_key, $operator, $foreign_key, $type, $where);

            // The join above is to the intimidatory table. This joins the query to the actual model.
            if ($method === 'BelongsToMany') {
                $related_foreign_key = $model->getQualifiedRelatedKeyName();
                $related_relation = $model->getRelated();
                $related_table = $related_relation->getTable();
                $related_qualified_key_name = $related_relation->getQualifiedKeyName();
                $this->query->join($related_table, $related_qualified_key_name, $operator, $related_foreign_key, $type, $where);
            }
        }

        // Group by the original model.
        $this->query->groupBy($this->model->getQualifiedKeyName());
    }
public function modelJoin ( $ relationships , $ operator = '=' , $ type = 'left' , $ where = false ) { if ( ! is_array ( $ relationships ) ) { $ relationships = [ $ relationships ] ; } if ( empty ( $ this -> query -> columns ) ) { $ this -> query -> selectRaw ( 'DISTINCT ' . $ this -> model -> getTable ( ) . '.*' ) ; } foreach ( $ relationships as $ relation_name => $ load_relationship ) { // Required variables. $ model = Arr :: get ( $ this -> relationships , $ relation_name . '.model' ) ; $ method = Arr :: get ( $ this -> relationships , $ relation_name . '.method' ) ; $ table = Arr :: get ( $ this -> relationships , $ relation_name . '.table' ) ; $ parent_key = Arr :: get ( $ this -> relationships , $ relation_name . '.parent_key' ) ; $ foreign_key = Arr :: get ( $ this -> relationships , $ relation_name . '.foreign_key' ) ; // Add the columns from the other table. // @todo do we need this? //$this->query->addSelect(new Expression("`$table`.*")); $ this -> query -> join ( $ table , $ parent_key , $ operator , $ foreign_key , $ type , $ where ) ; // The join above is to the intimidatory table. This joins the query to the actual model. if ( $ method === 'BelongsToMany' ) { $ related_foreign_key = $ model -> getQualifiedRelatedKeyName ( ) ; $ related_relation = $ model -> getRelated ( ) ; $ related_table = $ related_relation -> getTable ( ) ; $ related_qualified_key_name = $ related_relation -> getQualifiedKeyName ( ) ; $ this -> query -> join ( $ related_table , $ related_qualified_key_name , $ operator , $ related_foreign_key , $ type , $ where ) ; } } // Group by the original model. $ this -> query -> groupBy ( $ this -> model -> getQualifiedKeyName ( ) ) ; }
This determines the foreign key relations automatically to prevent the need to figure out the columns.

@param string $relation_name
@param string $operator
@param string $type
@param bool   $where

@return Builder

@SuppressWarnings(PHPMD.BooleanArgumentFlag)
@SuppressWarnings(PHPMD.LongVariable)
@SuppressWarnings(PHPMD.StaticAccess)
This determines the foreign key relations automatically to prevent the need to figure out the columns .