diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 85a71966cce0..c6039359f048 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -1729,6 +1729,7 @@ public function replicate(?array $except = null) $this->getCreatedAtColumn(), $this->getUpdatedAtColumn(), ...$this->uniqueIds(), + 'laravel_through_key', ])); $attributes = Arr::except( diff --git a/tests/Integration/Database/EloquentHasManyThroughTest.php b/tests/Integration/Database/EloquentHasManyThroughTest.php index 6dcdb9b8ae90..68e8363182b4 100644 --- a/tests/Integration/Database/EloquentHasManyThroughTest.php +++ b/tests/Integration/Database/EloquentHasManyThroughTest.php @@ -366,6 +366,21 @@ public function testUpdateOrCreateAffectingWrongModelsRegression() // $jane should not be updated, because it belongs to a different user altogether. $this->assertSame('jane-slug', $jane->fresh()->slug); } + + public function testCanReplicateModelLoadedThroughHasManyThrough() + { + $team = Team::create(); + $user = User::create(['team_id' => $team->id, 'name' => 'John']); + Article::create(['user_id' => $user->id, 'title' => 'John\'s new has-many-through-article']); + + $article = $team->articles()->first(); + + $this->assertInstanceOf(Article::class, $article); + + $newArticle = $article->replicate(); + $newArticle->title .= ' v2'; + $newArticle->save(); + } } class User extends Model