Skip to content

Instantly share code, notes, and snippets.

@twmbx
Last active January 1, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twmbx/8173929 to your computer and use it in GitHub Desktop.
Save twmbx/8173929 to your computer and use it in GitHub Desktop.
Adding utility timestamps to a Laravel 4 model. ( http://laravel.com )
<?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