Add the constraints for a relationship query on the same table.

@param Builder  $query
@param Builder  $parent
@param  array|mixed  $columns
@return Builder
963318
public function getRelationQueryForSelfJoin(Builder $query, Builder $parent, $columns = ['*'])
	{
		$query->select($columns);

		$query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash());

		$this->related->setTable($hash);

		$this->setJoin($query);

		return parent::getRelationQuery($query, $parent, $columns);
	}
780651
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->join($this->throughChild->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->ownerKey);

        if ($this->throughChildSoftDeletes()) {
            $query->whereNull($this->throughChild->getQualifiedDeletedAtColumn());
        }

        $query->getModel()->setTable($hash);

        return $query->whereColumn(
            $hash.'.'.$query->getModel()->qualifyColumn($this->secondKey), '=', $this->getQualifiedSecondOwnerKeyName()
        );
    }
875392
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
    {
        return $this->getRelationExistenceQuery($query, $parent, $columns);
    }
352149
protected function constraint($class, array $columns)
    {
        $constraint = new $class($this->getBuilder(), $columns);

        $this->queue($constraint);

        return $constraint;
    }
159955
public function getRelationCountQuery(Query $query, Query $parent)
    {
        $query->select(new Expression('count(*)'));

        $key = $this->wrap($this->getQualifiedParentKeyName());

        return $query->where($this->getHasCompareKey(), '=', new Expression($key));
    }
291184
public static function addRelationship($query, $relationship)
    {
        $property = $query->getRepository()->getDescription()->getProperty($relationship);

        switch (true) {
            case $property->isManyToOne() :
                return self::_parseManyToOne($query, $property);
            case $property->isManyToMany() :
                return self::_parseManyToMany($query, $property);
            case $property->isOneToMany() :
                return self::_parseOneToMany($query, $property);

        }
    }
626196
private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey)
    {
        $joinType = config('columnsortable.join_type', 'leftJoin');

        return $query->select($parentTable.'.*')->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
    }
888077
public function addConstraints()
    {
        if (static::$constraints) {
            $foreignKey = $this->getForeignKeyName();
            $parentKeyValue = $this->getParentKey();

            //If the foreign key is an array (multi-column relationship), we adjust the query.
            if (is_array($this->foreignKey)) {
                foreach ($this->foreignKey as $index => $key) {
                    list(, $key) = explode('.', $key);
                    $fullKey = $this->getRelated()
                            ->getTable().'.'.$key;
                    $this->query->where($fullKey, '=', $parentKeyValue[$index]);
                    $this->query->whereNotNull($fullKey);
                }
            } else {
                $fullKey = $this->getRelated()
                        ->getTable().'.'.$foreignKey;
                $this->query->where($fullKey, '=', $parentKeyValue);
                $this->query->whereNotNull($fullKey);
            }
        }
    }
708841
protected function joinSegment(Builder $query, $segment, EloquentModel $parent)
    {
        $relation = $parent->{$segment}();
        $related  = $relation->getRelated();
        $table    = $related->getTable();

        // If the table has been already joined let's skip it. Otherwise we will left join
        // it in order to allow using some query methods on mapped columns. Polymorphic
        // relations require also additional constraints, so let's handle it as well.
        if (!$this->alreadyJoined($query, $table)) {
            list($fk, $pk) = $this->getJoinKeys($relation);

            $query->leftJoin($table, function ($join) use ($fk, $pk, $relation, $parent, $related) {
                $join->on($fk, '=', $pk);

                if ($relation instanceof MorphOne || $relation instanceof MorphTo) {
                    $morphClass = ($relation instanceof MorphOne)
                        ? $parent->getMorphClass()
                        : $related->getMorphClass();

                    $join->where($relation->getQualifiedMorphType(), '=', $morphClass);
                }
            });
        }

        return [$table, $related];
    }
1326257
public function getRelationCountQuery(Builder $query, Builder $parent)
    {
        return $this->getRelationExistenceQuery($query, $parent, new Expression('count(*)'));
    }
90077
protected function performJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $farKey = $this->getQualifiedFarKeyName();

        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);

        if ($this->throughParentSoftDeletes()) {
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
        }
    }
1085828
public function getRelationExistenceQuery(Query $query, Query $parentQuery, $columns = ['*']) {
        return $query->select($columns)->whereColumn(
            $this->parent->qualifyColumn($this->parent->getKeyName()), '=', $this->getExistenceCompareKey()
        );
    }
1366318
public function applyConstraints($constraints, EloquentBuilder &$query, Field $fieldObject)
	{
		$configConstraints = $fieldObject->getOption('constraints');

		if (sizeof($configConstraints))
		{
			//iterate over the config constraints
			foreach ($configConstraints as $key => $relationshipName)
			{
				//now that we're looping through the constraints, check to see if this one was supplied
				if (isset($constraints[$key]) && $constraints[$key] && sizeof($constraints[$key]))
				{
					//first we get the other model and the relationship field on it
					$model = $this->config->getDataModel();
					$relatedModel = $model->{$fieldObject->getOption('field_name')}()->getRelated();
					$otherModel = $model->{$key}()->getRelated();

					//set the data model for the config
					$this->config->setDataModel($otherModel);
					$otherField = $this->make($relationshipName, array('type' => 'relationship'), false);

					//constrain the query
					$otherField->constrainQuery($query, $relatedModel, $constraints[$key]);

					//set the data model back to the original
					$this->config->setDataModel($model);
				}
			}
		}
	}
523167
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		if ($parent->getQuery()->from == $query->getQuery()->from) {
			return $this->getRelationQueryForSelfJoin($query, $parent, $columns);
		}

		$this->setJoin($query);

		return parent::getRelationQuery($query, $parent, $columns);
	}
93639
public function getRelationCountQuery(Query $query, Query $parent)
    {
        $query = parent::getRelationCountQuery($query, $parent);

        return $query->where($this->table.'.'.$this->morphType, $this->morphClass);
    }
863853
public function getRelationCountQuery(Builder $query, Builder $parent)
	{
		if ($parent->getQuery()->from == $query->getQuery()->from)
		{
			return $this->getRelationCountQueryForSelfRelation($query, $parent);
		}

		$query->select(new Expression('count(*)'));

		$otherKey = $this->wrap($query->getModel()->getTable().'.'.$this->otherKey);

		return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
	}
393944
protected function compileJoins(Builder $query, $joins)
    {
        return collect($joins)->map(function ($join) use ($query) {
            $table = $this->wrapTable($join->table);

            $nestedJoins = is_null($join->joins) ? '' : ' '.$this->compileJoins($query, $join->joins);

            $tableAndNestedJoins = is_null($join->joins) ? $table : '('.$table.$nestedJoins.')';

            return trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}");
        })->implode(' ');
    }