protected function joinManyToManyRelation(Relations\BelongsToMany $relation, $type) { $pivotTable = $relation->getTable(); // $relation->getQualifiedParentKeyName() is protected $parentKey = $relation->getParent()->getQualifiedKeyName(); $localKey = $relation->getOtherKey(); $this->query->join($pivotTable, $localKey, '=', $parentKey, $type); $related = $relation->getRelated(); $foreignKey = $relation->getForeignKey(); $relatedTable = $related->getTable(); $relatedKey = $related->getQualifiedKeyName(); $this->query->join($relatedTable, $foreignKey, '=', $relatedKey, $type); }
/** * Init nested query for filter. * * @param QueryBuilder $query * @param array $ids * * @return void */ protected function initNestedQuery(QueryBuilder $query, array $ids) { $connection = $query->getConnection(); $keyName = $connection->raw($this->relation->getParent()->getQualifiedKeyName()); $query->from($this->relation->getTable())->select($connection->raw('1'))->where($this->relation->getForeignKey(), '=', $keyName)->whereIn($this->relation->getOtherKey(), $ids); }
private function generateJoinFromBelongsToMany(BelongsToMany $relation) { // Pivot Table Generated On $generatedOnClause = [[$relation->getForeignKey(), $relation->getQualifiedParentKeyName()]]; // Pivot Table Join $this->generateJoin($relation->getTable(), $generatedOnClause); // Related Table Generated On $generatedOnClause = [$this->related->getQualifiedKeyName(), $relation->getOtherKey()]; // Related Table Join $this->generateJoin($this->related->getTable(), $this->compileOns($generatedOnClause), $this->nested ? $this->conditions[$this->index] : $this->conditions); }
protected function applyBelongsToMany(Query $query, Model $model, BelongsToMany $belongsToMany, $name) { if (isset($this->joinClasses[$name])) { return; } $modelTable = $model->getTable(); $related = $belongsToMany->getRelated(); $pivotTable = $belongsToMany->getTable(); $pivotAlias = $pivotTable . '_pivot'; $pivotLocalKey = $belongsToMany->getOtherKey(); $relatedTable = $related->getTable(); $relationKey = $related->getKeyName(); $foreignKey = $belongsToMany->getForeignKey(); $qualifiedLocalKey = $belongsToMany->getQualifiedParentKeyName(); $alias = $this->joinNameToAlias($name); $joinMethod = $this->getJoinMethod($name); $query->{$joinMethod}("{$pivotTable}", "{$qualifiedLocalKey}", '=', "{$foreignKey}"); $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$relatedTable}.{$relationKey}", '=', "{$pivotLocalKey}"); $query->distinct(); $this->joinClasses[$name] = $related; $this->joinTable[$name] = $relatedTable; $this->joinAliases[$name] = $alias; }