Skip to content

Commit

Permalink
Merge pull request #42 from Sharptsa/use_ownerkey_localkey
Browse files Browse the repository at this point in the history
Use the localKey or the ownerKey of the relation
  • Loading branch information
fico7489 authored Jan 19, 2019
2 parents 0d1eced + 4edde49 commit 2fe6cf8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/EloquentJoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,22 @@ private function performJoin($relations, $leftJoin = null)
$joinQuery = $relatedTable.($this->useTableAlias ? ' as '.$relatedTableAlias : '');
if ($relatedRelation instanceof BelongsToJoin) {
$relatedKey = $relatedRelation->getForeignKey();
$ownerKey = $relatedRelation->getOwnerKey();

$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedPrimaryKey, $currentTableAlias, $relatedKey) {
$join->on($relatedTableAlias.'.'.$relatedPrimaryKey, '=', $currentTableAlias.'.'.$relatedKey);
$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $ownerKey, $currentTableAlias, $relatedKey) {
$join->on($relatedTableAlias.'.'.$ownerKey, '=', $currentTableAlias.'.'.$relatedKey);

$this->joinQuery($join, $relatedRelation, $relatedTableAlias);
});
} elseif ($relatedRelation instanceof HasOneJoin || $relatedRelation instanceof HasManyJoin) {
$relatedKey = $relatedRelation->getQualifiedForeignKeyName();
$relatedKey = last(explode('.', $relatedKey));

$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedPrimaryKey, $currentTableAlias, $relatedKey, $currentPrimaryKey) {
$join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$currentPrimaryKey);
$localKey = $relatedRelation->getQualifiedParentKeyName();
$localKey = last(explode('.', $localKey));

$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $localKey) {
$join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$localKey);

$this->joinQuery($join, $relatedRelation, $relatedTableAlias);
});
Expand Down

0 comments on commit 2fe6cf8

Please sign in to comment.