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