From a56dadb9ab869831bda8a8e8c3329a4e33e252c4 Mon Sep 17 00:00:00 2001 From: Filip Horvat Date: Fri, 23 Feb 2018 11:54:04 +0100 Subject: [PATCH] fix HasOneJoin current table primary key --- src/Traits/EloquentJoinTrait.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Traits/EloquentJoinTrait.php b/src/Traits/EloquentJoinTrait.php index 9b49bf1..b95504f 100644 --- a/src/Traits/EloquentJoinTrait.php +++ b/src/Traits/EloquentJoinTrait.php @@ -90,8 +90,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) { @@ -124,8 +125,8 @@ private function performJoin($builder, $relations) $keyRelated = $relatedRelation->getForeignKey(); $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); }); @@ -134,8 +135,9 @@ private function performJoin($builder, $relations) } } - $currentTable = $relatedTableAlias; $currentModel = $relatedModel; + $currentPrimaryKey = $relatedPrimaryKey; + $currentTable = $relatedTableAlias; $this->joinedTables[$relation] = $relatedTableAlias; }