Add subselect queries to count the relations.

@param  mixed  $relations
@return $this
507828
public function withCount($relations)
    {
        if (is_string($relations)) $relations = func_get_args();

        // If no columns are set, add the default * columns.
        if (is_null($this->query->columns)) {
            $this->query->select($this->query->from .'.*');
        }

        $relations = $this->parseRelations($relations);

        foreach ($relations as $name => $constraints) {
            $segments = explode(' ', $name);

            if ((count($segments) == 3) && (Str::lower($segments[1]) == 'as')) {
                list($name, $alias) = array($segments[0], $segments[2]);
            } else {
                unset($alias);
            }

            $relation = $this->getHasRelationQuery($name);

            // Here we will get the relationship count query and prepare to add it to the main query
            // as a sub-select. First, we'll get the "has" query and use that to get the relation
            // count query. We will normalize the relation name then append _count as the name.
            $query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);

            call_user_func($constraints, $query);

            $this->mergeWheresToHas($query, $relation);

            $asColumn = snake_case(isset($alias) ? $alias : $name) .'_count';

            $this->selectSub($query->getQuery(), $asColumn);
        }

        return $this;
    }
545039
public function setProxies(array $relations = null)
    {
        $attributes = $this->getEntityAttributes();
        $proxies = [];

        $relations = $this->getRelationsToProxy();

        // Before calling the relationship methods, we'll set the relationship
        // method to null, to avoid hydration error on class properties
        foreach ($relations as $relation) {
            $this->setEntityAttribute($relation, null);
        }

        foreach ($relations as $relation) {
            // First, we check that the relation has not been already
            // set, in which case, we'll just pass.
            if (array_key_exists($relation, $attributes) && !is_null($attributes[$relation])) {
                continue;
            }

            // If the key is handled locally and we know it not to be set,
            // we'll set the relationship to null value
            if (!$this->relationNeedsProxy($relation, $attributes)) {
                $proxies[$relation] = $this->entityMap->getEmptyValueForRelationship($relation);
            } else {
                $targetClass = $this->getClassToProxy($relation, $attributes);
                $proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass);
            }
        }

        foreach ($proxies as $key => $value) {
            $this->setEntityAttribute($key, $value);
        }
    }
1078933
protected function parseWithRelations( array $relations )
	{
		$results = [];

		foreach ( $relations as $name => $constraints )
		{
			// If the "relation" value is actually a numeric key, we can assume that no
			// constraints have been specified for the eager load and we'll just put
			// an empty Closure with the loader so that we can treat all the same.
			if ( is_numeric( $name ) )
			{
				$f = function ()
				{
					//
				};

				list( $name, $constraints ) = [$constraints, $f];
			}

			// We need to separate out any nested includes. Which allows the developers
			// to load deep relationships using "dots" without stating each level of
			// the relationship with its own key in the array of eager load names.
			$results = $this->parseNestedWith( $name, $results );

			$results[$name] = $constraints;
		}

		return $results;
	}
566768
public static function create($models, array $relations) {
        $is_one = !static::isSomeArr($models);
        if ($is_one) {
            $models = [$models];
        }
        foreach ($relations as $key => $relation) {
            $relation = static::parse($relation, $key);
            $models =  $relation->getWithMulti($models);
        }
        return $is_one ? reset($models) : $models;
    }
982527
protected function joinForSearch($mappings, $subquery)
    {
        $mappings = is_array($mappings) ? $mappings : (array) $mappings;

        $columns = new ColumnCollection;

        $grammar = $this->query->getGrammar();

        $joiner = static::$joinerFactory->make($subquery->getQuery(), $this->model);

        // Here we loop through the search mappings in order to join related tables
        // appropriately and build a searchable column collection, which we will
        // use to build select and where clauses with correct table prefixes.
        foreach ($mappings as $mapping => $weight) {
            if (strpos($mapping, '.') !== false) {
                list($relation, $column) = $this->model->parseMappedColumn($mapping);

                $related = $joiner->leftJoin($relation);

                $columns->add(
                    new Column($grammar, $related->getTable(), $column, $mapping, $weight)
                );
            } else {
                $columns->add(
                    new Column($grammar, $this->model->getTable(), $mapping, $mapping, $weight)
                );
            }
        }

        return $columns;
    }
1153960
protected function getResultsByRelatedKey($relations)
	{
		$resultsByRelatedKey = [];

		foreach ($relations as $relation)
		{
			if (!isset($resultsByRelatedKey[$relation->{$this->associativeLocal}]))
			{
				$resultsByRelatedKey[$relation->{$this->associativeLocal}] = new Rows;
			}

			$resultsByRelatedKey[$relation->{$this->associativeLocal}]->push($relation);
		}

		return $resultsByRelatedKey;
	}
430556
public function with(array $relations)
    {
        $this->with = array_unique(array_merge($this->with, $relations));

        return $this;
    }
241062
public function whereRelations(array $relations)
    {
        $instance = $this->whereStrategyInstance();

        $instance->whereStrategy()->whereRelations($relations);

        return $instance;
    }
178503
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, []);
        }
        return $models;
    }
800304
protected function hasOneOrManyDeepFromRelations(array $relations)
    {
        if (is_array($relations[0])) {
            $relations = $relations[0];
        }

        $related = null;
        $through = [];
        $foreignKeys = [];
        $localKeys = [];

        foreach ($relations as $i => $relation) {
            $method = $this->hasOneOrManyDeepRelationMethod($relation);

            [$through, $foreignKeys, $localKeys] = $this->$method($relation, $through, $foreignKeys, $localKeys);

            if ($i === count($relations) - 1) {
                $related = get_class($relation->getRelated());
            } else {
                $through[] = get_class($relation->getRelated());
            }
        }

        return [$related, $through, $foreignKeys, $localKeys];
    }
684938
public function loadMissing($relations)
    {
        $relations = is_string($relations) ? func_get_args() : $relations;

        $this->newCollection([$this])->loadMissing($relations);

        return $this;
    }
876074
protected function addManyRelation(string $relation)
    {
        if (!in_array($relation, $this->manyRelations)) {
            $this->manyRelations[] = $relation;
        }
    }
501715
public function with($relation)
    {
        foreach ((array) $relation as $node) {
            $instance = $this->factory->build($this->model(), $node);
            $this->relations[$instance->name()] = $instance;
        }

        return $this;
    }
150140
public function with($relation)
    {
        if (func_num_args() == 1) {
            $this->with = array_merge($this->with, is_array($relation) ? $relation : [$relation]);
        } else {
            $this->with = array_merge($this->with, func_get_args());
        }

        return $this;
    }
1300471
public function addOrders(array $orders)
    {
        foreach ($orders as $key => $order) {
            $this->addOrder($order);

            /** Name the expression if $key is a non-numeric string. */
            if (is_string($key) && !is_numeric($key)) {
                $order = end($this->orders);
                $order->setName($key);
            }
        }

        return $this;
    }
652137
public function rightJoinRelations($relations)
    {
        $relations = is_array($relations) ? $relations : func_get_args();

        return $this->joinRelations($relations, 'right');
    }
575916
public function with($relationships)
    {
        if (! is_array($relationships)) {
            $relationships = explode(', ', $relationships);
        }

        if (! in_array($relationships, $this->withRelationships)) {
            foreach ($relationships as $with) {
                $this->withRelationships[] = $with;
            }
        }

        return $this;
    }