diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 70cf998aae3d..7992f622d2a3 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -227,10 +227,14 @@ protected function updateExistingPivotUsingCustomClass($id, array $attributes, $ $updated = $pivot ? $pivot->fill($attributes)->isDirty() : false; - $this->newPivot([ + $pivot = $this->newPivot([ $this->foreignPivotKey => $this->parent->{$this->parentKey}, $this->relatedPivotKey => $this->parseId($id), - ], true)->fill($attributes)->save(); + ], true); + + $pivot->timestamps = in_array($this->updatedAt(), $this->pivotColumns); + + $pivot->fill($attributes)->save(); if ($touch) { $this->touchIfTouching(); diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index f74f33fc0d69..2dacc9a08f5e 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -187,9 +187,15 @@ public function test_custom_pivot_class_using_update_existing_pivot() $tag = TagWithCustomPivot::create(['name' => Str::random()]); DB::table('posts_tags')->insert([ - ['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty'], + [ + 'post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty', + 'created_at' => '1507630210', + 'updated_at' => '1507630210', + ], ]); + Carbon::setTestNow('2017-10-10 10:10:20'); // +10 seconds + // Test on actually existing pivot $this->assertEquals( 1, @@ -197,6 +203,8 @@ public function test_custom_pivot_class_using_update_existing_pivot() ); foreach ($post->tagsWithCustomExtraPivot as $tag) { $this->assertEquals('exclude', $tag->pivot->flag); + $this->assertEquals('1507630210', $tag->pivot->getAttributes()['created_at']); + $this->assertEquals('1507630220', $tag->pivot->getAttributes()['updated_at']); // +10 seconds } // Test on non-existent pivot