Skip to content

Commit

Permalink
Refresh updated_at timestamp on soft delete (#19538)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakym authored and taylorotwell committed Jun 9, 2017
1 parent fd40d0c commit a1395f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion tests/Database/DatabaseSoftDeletingTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -53,6 +56,7 @@ class DatabaseSoftDeletingTraitStub
{
use \Illuminate\Database\Eloquent\SoftDeletes;
public $deleted_at;
public $updated_at;

public function newQuery()
{
Expand Down Expand Up @@ -93,4 +97,9 @@ public function fromDateTime()
{
return 'date-time';
}

public function getUpdatedAtColumn()
{
return defined('static::UPDATED_AT') ? static::UPDATED_AT : 'updated_at';
}
}

0 comments on commit a1395f2

Please sign in to comment.