From 6b73974505557c9663f9f6cbc00793d3225c9d20 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Mon, 3 Sep 2012 21:44:16 +0100 Subject: [PATCH 1/3] Added new touch function to eloquent, updates timestamps and immediately saves Signed-off-by: Callum McIntyre --- laravel/database/eloquent/model.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 306d471a..c2566496 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -452,6 +452,16 @@ abstract class Model { if ( ! $this->exists) $this->created_at = $this->updated_at; } + /** + *Updates the timestamp on the model and immediately saves it. + * + * @return void + */ + public function touch(){ + $this->timestamp(); + $this->save(); + } + /** * Get a new fluent query builder instance for the model. * From 0161273010fd5217d0f0b2e1ee03066d2204fcb8 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Mon, 3 Sep 2012 22:23:58 +0100 Subject: [PATCH 2/3] Added documentation for the touch() function Signed-off-by: Callum McIntyre --- laravel/documentation/database/eloquent.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index cbcf83c9..45969c79 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -132,6 +132,11 @@ Need to maintain creation and update timestamps on your database records? With E 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. +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: + + $comment = Comment::find(1); + $comment->touch(); + > **Note:** You can change the default timezone of your application in the **application/config/application.php** file. From c8ee7ca6148942be2ccff7e26518721425f80247 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Mon, 3 Sep 2012 23:23:36 +0100 Subject: [PATCH 3/3] Added documentation regarding the newly public timestamp() function Signed-off-by: Callum McIntyre --- laravel/documentation/database/eloquent.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index 45969c79..09493db0 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -137,6 +137,13 @@ In some cases it may be useful to update the **updated_at** date column without $comment = Comment::find(1); $comment->touch(); +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: + + $comment = Comment::find(1); + $comment->timestamp(); + //do something else here, but not modifying the $comment model data + $comment->save(); + > **Note:** You can change the default timezone of your application in the **application/config/application.php** file.