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; }