Skip to content

Instantly share code, notes, and snippets.

@arunbsar
Last active November 24, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save arunbsar/c63660b76f3cd5794c916ce703a0a549 to your computer and use it in GitHub Desktop.
Save arunbsar/c63660b76f3cd5794c916ce703a0a549 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 static function boot() {
public $timestamps = false;
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