Last active
January 1, 2016 16:59
-
-
Save twmbx/8173929 to your computer and use it in GitHub Desktop.
Adding utility timestamps to a Laravel 4 model. ( http://laravel.com )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this assumes that you have a model that is maintaining timestamps using Eloquent | |
class User { | |
// So this is what tells laravel to update your timestamps for you | |
// That means you have the columns 'created_at' & 'updated_at' | |
public $timestamps = true; | |
// If you want to maintain an extra column for a users last login without | |
// touching the 'updated_at' column on the model's save call you can do the following. | |
// obviously the toggling of the timestamps value could | |
// be streamlined by putting it into a function. | |
// particularly if you maintain a base class for all your models | |
public function touchLoginTimestamp() | |
{ | |
$this->timestamps = false; | |
$this->last_login = date('Y-m-d H:i:s'); | |
// this is just my alternative to save().... | |
$this->amend(); | |
$this->timestamps = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment