Merge all the relation queries into a single query with UNION ALL.

@param  \Illuminate\Support\Collection  $relations
@return \Illuminate\Database\Eloquent\Builder
173424
protected function unionAllRelationQueries(Collection $relations): EloquentBuilder
    {
        return $relations
            ->reduce(
                function (EloquentBuilder $builder, Relation $relation) {
                    return $builder->unionAll(
                        $relation->getQuery()
                    );
                },
                // Use the first query as the initial starting point
                $relations->shift()->getQuery()
            );
    }
777891
protected function mergeRelationships(array $relationships)
    {
        foreach ($this->merged as $resource) {
            $relationships = array_replace_recursive($relationships, $resource->getRelationshipsAsArray());
        }

        return $relationships;
    }
1229060
public function getAll($relations = true)
    {
        $results = array_merge($this->data, [
            'fields' => $this->getFields(),
            'files' => $this->getFileFieldNames(),
            'json' => $this->getJsonFieldNames(),
            'listable' => $this->getListableFieldNames(),
            'detailable' => $this->getDetailableFieldNames(),
            'required' => $this->getRequiredFieldNames(),
            'slugable' => $this->getSlugableFieldNames(),
            'uuids' => $this->getUuidFieldNames(),
            'unique' => $this->getUniqueFieldNames()
        ]);

        return $results;
    }
377401
public function loadCount($relations)
    {
        if ($this->isEmpty()) {
            return $this;
        }

        $models = $this->first()->newModelQuery()
            ->whereKey($this->modelKeys())
            ->select($this->first()->getKeyName())
            ->withCount(...func_get_args())
            ->get();

        $attributes = Arr::except(
            array_keys($models->first()->getAttributes()),
            $models->first()->getKeyName()
        );

        $models->each(function ($model) use ($attributes) {
            $this->find($model->getKey())->forceFill(
                Arr::only($model->getAttributes(), $attributes)
            )->syncOriginalAttributes($attributes);
        });

        return $this;
    }
5672
public function withCount( $relations )
	{
		if ( is_null( $this->query->columns ) )
		{
			$this->query->select( ['*'] );
		}

		$relations = is_array( $relations ) ? $relations : func_get_args();

		foreach ( $this->parseWithRelations( $relations ) as $name => $constraints )
		{
			// 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.
			$relation = $this->getHasRelationQuery( $name );

			$query = $relation->getRelationCountQuery( $relation->getRelated()->newQuery(), $this );

			$query->callScope( $constraints );

			$query->mergeModelDefinedRelationConstraints( $relation->getQuery() );

			$this->selectSub( $query->toBase(), snake_case( $name ) . '_count' );
		}

		return $this;
	}
537949
protected function getHasRelationQuery($relation)
	{
		$me = $this;

		return Relation::noConstraints(function() use ($me, $relation)
		{
			return $me->getModel()->$relation();
		});
	}
966549
public function with($relations)
    {
        $this->with = $relations;
        $this->fields = array_merge($this->fields, $this->with);
        return $this;
    }
395904
public function matchMany(array $models, Collection $results, $relation)
    {
        return $this->matchOneOrMany($models, $results, $relation, 'many');
    }
871340
public function transform($relations = false)
    {
        $relations = $relations ? Config::get('larasearch.paths.' . get_class($this)) : [];

        $doc = $this->load($relations)->toArray();

        return $doc;
    }
1326946
protected function associateRelationModels(string $relationName, EloquentCollection $relationModels): self
    {
        $relation = $this->getRelationInstance($relationName);

        $relation->match(
            $this->models->all(),
            $relationModels,
            $relationName
        );

        return $this;
    }
926408
public function havingRelations(array $relations)
    {
        $instance = $this->havingStrategyInstance();

        $instance->havingStrategy()->havingRelations($relations);

        return $instance;
    }
711064
public function match(array $models, Collection $results, $relation)
    {
        // This relationship deals with a One-To-One morph type so we'll just extract
        // the first model out of the results and return it.
        $matched = parent::match($models, $results, $relation);

        return array_map(function($match) use ($relation)
        {
            if (isset($match[$relation]) && isset($match[$relation][0]))
            {
                $match->setRelation($relation, $match[$relation][0]);
            }

            return $match;

        }, $matched);
    }
996468
protected function getRelationMethods(array $relations, array $fields)
    {
        $methods = [];

        foreach ($fields as $field) {
            if ($field->hasForeignRelation()) {
                $relation = $field->getForeignRelation();
                $methods[$relation->name] = $this->getRelationshipMethod($relation);
            }
        }

        foreach ($relations as $relation) {
            $methods[$relation->name] = $this->getRelationshipMethod($relation);
        }

        return $methods;
    }
1285819
public static function buildRelationQuery($entity, Relation $relation) {
        $relQuery = "";
        $getid = "getId";
        if ($relation->getCardinality() == Relation::$manyToMany) {
            $m = "get" . $relation->getName();
            $relQuery .= "INSERT INTO " . $relation->getTable() . " (" . $relation->getLocalColumn() . ", " . $relation->getForeignColumn() . ") ";
            $relQuery .= "VALUES (:" . $relation->getLocalColumn() . ", :" . $relation->getForeignColumn() . ");";
        }
        if ($relation->getCardinality() == Relation::$oneToMany) {
            $relMapper = MapperBuilder::buildFromName($this->mapping, $relation->getEntity());
            $m = "get" . $relation->getName();
            foreach ($entity->$m() as $rel) {
                $setid = "set" . $relMapper->getNameForColumn($relation->getForeignColumn());
                $rel->$setid($entity->$getid());
                $relQuery .= $this->buildInsertQuery($rel);
            }
        }


        return $relQuery;
    }
84206
public function with($relationships)
    {
        foreach ($this->resources as $resource) {
            $resource->with($relationships);
        }

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

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

        return $instance;
    }
160794
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->getDefaultFor($model));
        }

        return $models;
    }