Skip to content

Commit ac08f1e

Browse files
committed
[feat] allow touch in multiple columns at the sametime
1 parent d2b2cdc commit ac08f1e

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,15 +1298,17 @@ public function upsert(array $values, $uniqueBy, $update = null)
12981298
/**
12991299
* Update the column's update timestamp.
13001300
*
1301-
* @param string|null $column
1301+
* @param string|array|null $column
13021302
* @return int|false
13031303
*/
13041304
public function touch($column = null)
13051305
{
13061306
$time = $this->model->freshTimestamp();
13071307

13081308
if ($column) {
1309-
return $this->toBase()->update([$column => $time]);
1309+
$columns = (new BaseCollection(Arr::wrap($column)))->mapWithKeys(fn ($column) => [$column => $time])->all();
1310+
1311+
return $this->toBase()->update($columns);
13101312
}
13111313

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

src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Concerns;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Facades\Date;
67

78
trait HasTimestamps
@@ -23,13 +24,17 @@ trait HasTimestamps
2324
/**
2425
* Update the model's update timestamp.
2526
*
26-
* @param string|null $attribute
27+
* @param string|array|null $attribute
2728
* @return bool
2829
*/
2930
public function touch($attribute = null)
3031
{
3132
if ($attribute) {
32-
$this->$attribute = $this->freshTimestamp();
33+
$time = $this->freshTimestamp();
34+
35+
foreach (Arr::wrap($attribute) as $col) {
36+
$this->$col = $time;
37+
}
3338

3439
return $this->save();
3540
}
@@ -46,7 +51,7 @@ public function touch($attribute = null)
4651
/**
4752
* Update the model's update timestamp without raising any events.
4853
*
49-
* @param string|null $attribute
54+
* @param string|array|null $attribute
5055
* @return bool
5156
*/
5257
public function touchQuietly($attribute = null)

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,25 @@ public function testTouchWithCustomColumn()
27402740
$this->assertEquals(2, $result);
27412741
}
27422742

2743+
public function testTouchWithMultipleColumns()
2744+
{
2745+
Carbon::setTestNow($now = '2017-10-10 10:10:10');
2746+
2747+
$query = m::mock(BaseBuilder::class);
2748+
$query->shouldReceive('from')->with('foo_table')->andReturn('foo_table');
2749+
$query->from = 'foo_table';
2750+
2751+
$builder = new Builder($query);
2752+
$model = new EloquentBuilderTestStubStringPrimaryKey;
2753+
$builder->setModel($model);
2754+
2755+
$query->shouldReceive('update')->once()->with(['published_at' => $now, 'verified_at' => $now])->andReturn(2);
2756+
2757+
$result = $builder->touch(['published_at', 'verified_at']);
2758+
2759+
$this->assertEquals(2, $result);
2760+
}
2761+
27432762
public function testTouchWithoutUpdatedAtColumn()
27442763
{
27452764
$query = m::mock(BaseBuilder::class);

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,20 @@ protected function addMockConnection($model)
32553255
});
32563256
}
32573257

3258+
public function testTouchMethodWithMultipleAttributes()
3259+
{
3260+
Carbon::setTestNow($now = Carbon::now());
3261+
3262+
$model = m::mock(EloquentModelStub::class.'[save]');
3263+
$model->shouldReceive('save')->once()->andReturn(true);
3264+
3265+
$result = $model->touch(['published_at', 'verified_at']);
3266+
3267+
$this->assertTrue($result);
3268+
$this->assertEquals($now->toDateTimeString(), $model->published_at->toDateTimeString());
3269+
$this->assertEquals($now->toDateTimeString(), $model->verified_at->toDateTimeString());
3270+
}
3271+
32583272
public function testTouchingModelWithTimestamps()
32593273
{
32603274
$this->assertFalse(

0 commit comments

Comments
 (0)