Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add ability to Model::query()->touch() to mass update timestamps #43665

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,29 @@ public function upsert(array $values, $uniqueBy, $update = null)
);
}

/**
* Update the column's update timestamp.
*
* @param string|null $column
* @return int|false
*/
public function touch($column = null)
{
$time = $this->model->freshTimestamp();

if ($column) {
return $this->toBase()->update([$column => $time]);
stevebauman marked this conversation as resolved.
Show resolved Hide resolved
}

$column = $this->model->getUpdatedAtColumn();

if (! $this->model->usesTimestamps() || is_null($column)) {
return false;
}

return $this->toBase()->update([$column => $time]);
stevebauman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Increment a column's value by a given amount.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,61 @@ public function testUpsert()
$this->assertEquals(2, $result);
}

public function testTouch()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');

$query = m::mock(BaseBuilder::class);
$query->shouldReceive('from')->with('foo_table')->andReturn('foo_table');
$query->from = 'foo_table';

$builder = new Builder($query);
$model = new EloquentBuilderTestStubStringPrimaryKey;
$builder->setModel($model);

$query->shouldReceive('update')->once()->with(['updated_at' => $now])->andReturn(2);

$result = $builder->touch();

$this->assertEquals(2, $result);
}

public function testTouchWithCustomColumn()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');

$query = m::mock(BaseBuilder::class);
$query->shouldReceive('from')->with('foo_table')->andReturn('foo_table');
$query->from = 'foo_table';

$builder = new Builder($query);
$model = new EloquentBuilderTestStubStringPrimaryKey;
$builder->setModel($model);

$query->shouldReceive('update')->once()->with(['published_at' => $now])->andReturn(2);

$result = $builder->touch('published_at');

$this->assertEquals(2, $result);
}

public function testTouchWithoutUpdatedAtColumn()
{
$query = m::mock(BaseBuilder::class);
$query->shouldReceive('from')->with('table')->andReturn('table');
$query->from = 'table';

$builder = new Builder($query);
$model = new EloquentBuilderTestStubWithoutTimestamp;
$builder->setModel($model);

$query->shouldNotReceive('update');

$result = $builder->touch();

$this->assertFalse($result);
}

public function testWithCastsMethod()
{
$builder = new Builder($this->getMockQueryBuilder());
Expand Down