diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index ad86d832c653..93f781dcd007 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -503,7 +503,7 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder { $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()); - $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondLocalKey); + $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey); if ($this->throughParentSoftDeletes()) { $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn()); diff --git a/tests/Integration/Database/EloquentHasManyThroughTest.php b/tests/Integration/Database/EloquentHasManyThroughTest.php index 633da206bc70..06ce0acc2729 100644 --- a/tests/Integration/Database/EloquentHasManyThroughTest.php +++ b/tests/Integration/Database/EloquentHasManyThroughTest.php @@ -33,7 +33,7 @@ public function test_basic_create_and_retrieve() { $user = User::create(['name' => str_random()]); - $team1 = Team::create(['owner_id' => $user->id]); + $team1 = Team::create(['id' => 10, 'owner_id' => $user->id]); $team2 = Team::create(['owner_id' => $user->id]); $mate1 = User::create(['name' => str_random(), 'team_id' => $team1->id]); @@ -127,5 +127,5 @@ class Team extends Model { public $table = 'teams'; public $timestamps = false; - protected $guarded = ['id']; + protected $guarded = []; }