diff --git a/src/Traits/EloquentJoinTrait.php b/src/Traits/EloquentJoinTrait.php index 03d9a72..eccc199 100644 --- a/src/Traits/EloquentJoinTrait.php +++ b/src/Traits/EloquentJoinTrait.php @@ -88,8 +88,9 @@ private function performJoin($builder, $relations) $baseTable = $this->getTable(); $baseModel = $this; - $currentTable = $this->getTable(); $currentModel = $this; + $currentPrimaryKey = $this->getKeyName(); + $currentTable = $this->getTable(); foreach ($relations as $relation) { if ($relation == $column) { @@ -122,8 +123,8 @@ private function performJoin($builder, $relations) $keyRelated = $relatedRelation->getQualifiedForeignKeyName(); $keyRelated = last(explode('.', $keyRelated)); - $builder->leftJoin($joinQuery, function ($join) use ($relatedTableAlias, $keyRelated, $currentTable, $relatedPrimaryKey, $relatedModel) { - $join->on($relatedTableAlias . '.' . $keyRelated, '=', $currentTable . '.' . $relatedPrimaryKey); + $builder->leftJoin($joinQuery, function ($join) use ($relatedTableAlias, $keyRelated, $currentTable, $relatedPrimaryKey, $relatedModel, $currentPrimaryKey) { + $join->on($relatedTableAlias . '.' . $keyRelated, '=', $currentTable . '.' . $currentPrimaryKey); $this->leftJoinQuery($join, $relatedModel, $relatedTableAlias); }); @@ -132,8 +133,9 @@ private function performJoin($builder, $relations) } } - $currentTable = $relatedTableAlias; $currentModel = $relatedModel; + $currentPrimaryKey = $relatedPrimaryKey; + $currentTable = $relatedTableAlias; $this->joinedTables[$relation] = $relatedTableAlias; }