Skip to content

Commit 666db87

Browse files
committed
Merge pull request laravel#1169 from mcintyre94/minor/add-touch-eloquent
Added new touch function to eloquent, updates timestamps and immediately saves
2 parents f004ddc + c8ee7ca commit 666db87

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

laravel/database/eloquent/model.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ public function timestamp()
452452
if ( ! $this->exists) $this->created_at = $this->updated_at;
453453
}
454454

455+
/**
456+
*Updates the timestamp on the model and immediately saves it.
457+
*
458+
* @return void
459+
*/
460+
public function touch(){
461+
$this->timestamp();
462+
$this->save();
463+
}
464+
455465
/**
456466
* Get a new fluent query builder instance for the model.
457467
*

laravel/documentation/database/eloquent.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ Need to maintain creation and update timestamps on your database records? With E
132132

133133
Next, add **created_at** and **updated_at** date columns to your table. Now, whenever you save the model, the creation and update timestamps will be set automatically. You're welcome.
134134

135+
In some cases it may be useful to update the **updated_at** date column without actually modifying any data within the model. Simply use the **touch** method, which will also automatically save the changes immediately:
136+
137+
$comment = Comment::find(1);
138+
$comment->touch();
139+
140+
You can also use the **timestamp** function to update the **updated_at** date column without saving the model immediately. Note that if you are actually modifying the model's data this is handled behind the scenes:
141+
142+
$comment = Comment::find(1);
143+
$comment->timestamp();
144+
//do something else here, but not modifying the $comment model data
145+
$comment->save();
146+
135147
> **Note:** You can change the default timezone of your application in the **application/config/application.php** file.
136148
137149
<a name="relationships"></a>

0 commit comments

Comments
 (0)