From a1395f21be4fa701b2a160194de78f836c7b0c75 Mon Sep 17 00:00:00 2001 From: Dominic Date: Fri, 9 Jun 2017 13:56:17 +0100 Subject: [PATCH] Refresh updated_at timestamp on soft delete (#19538) --- src/Illuminate/Database/Eloquent/SoftDeletes.php | 10 ++++++++-- tests/Database/DatabaseSoftDeletingTraitTest.php | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index fdca75ce11f3..0cd5446383de 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -60,9 +60,15 @@ protected function runSoftDelete() { $query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey()); - $this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp(); + $time = $this->freshTimestamp(); - $query->update([$this->getDeletedAtColumn() => $this->fromDateTime($time)]); + $this->{$this->getDeletedAtColumn()} = $time; + $this->{$this->getUpdatedAtColumn()} = $time; + + $query->update([ + $this->getDeletedAtColumn() => $this->fromDateTime($time), + $this->getUpdatedAtColumn() => $this->fromDateTime($time) + ]); } /** diff --git a/tests/Database/DatabaseSoftDeletingTraitTest.php b/tests/Database/DatabaseSoftDeletingTraitTest.php index 1f255a8bbc25..077ee9717f26 100644 --- a/tests/Database/DatabaseSoftDeletingTraitTest.php +++ b/tests/Database/DatabaseSoftDeletingTraitTest.php @@ -19,7 +19,10 @@ public function testDeleteSetsSoftDeletedColumn() // $model->shouldReceive('newQuery')->andReturn($query = m::mock('StdClass')); $model->shouldReceive('newQueryWithoutScopes')->andReturn($query = m::mock('StdClass')); $query->shouldReceive('where')->once()->with('id', 1)->andReturn($query); - $query->shouldReceive('update')->once()->with(['deleted_at' => 'date-time']); + $query->shouldReceive('update')->once()->with([ + 'deleted_at' => 'date-time', + 'updated_at' => 'date-time' + ]); $model->delete(); $this->assertInstanceOf('Carbon\Carbon', $model->deleted_at); @@ -53,6 +56,7 @@ class DatabaseSoftDeletingTraitStub { use \Illuminate\Database\Eloquent\SoftDeletes; public $deleted_at; + public $updated_at; public function newQuery() { @@ -93,4 +97,9 @@ public function fromDateTime() { return 'date-time'; } + + public function getUpdatedAtColumn() + { + return defined('static::UPDATED_AT') ? static::UPDATED_AT : 'updated_at'; + } }