Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save playwellsteve/2702536a8df0efc025d1d02ce0a2d01c to your computer and use it in GitHub Desktop.
Save playwellsteve/2702536a8df0efc025d1d02ce0a2d01c to your computer and use it in GitHub Desktop.
While Laravel Eloquent class performs standard timestamp management, this script will work in ALL cases (such as migrations which are using the basic insert command). In addition, this script provides the ability to override the format being used to store timestamps.
$dt=new DateTime;$model->created_at=$dt->format('m-d-y H:i:s');
/*
* This block of code could be added to your models (or you can create a new model which contains these and extend from your
* model. Each section will make sure the created_at and updated_at timestamp fields are updated in the database.
*/
class YourModel extends Eloquent....
public $timestamps = false;
public static function boot() {
parent::boot();
static::creating(function($model) {
$dt = new DateTime();
$model->created_at = $dt->format('m-d-y H:i:s');
return true;
});
static::updating(function($model) {
$dt = new DateTime();
$model->updated_at = $dt->format('m-d-y H:i:s');
return true;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment