Browse Source

Merge pull request #1169 from mcintyre94/minor/add-touch-eloquent

Added new touch function to eloquent, updates timestamps and immediately saves
Dayle Rees 12 years ago
parent
commit
666db87137
2 changed files with 22 additions and 0 deletions
  1. 10 0
      laravel/database/eloquent/model.php
  2. 12 0
      laravel/documentation/database/eloquent.md

+ 10 - 0
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.
 	 *

+ 12 - 0
laravel/documentation/database/eloquent.md

@@ -132,6 +132,18 @@ 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();
+
+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.
 
 <a name="relationships"></a>