Last active
November 24, 2018 06:38
-
-
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.
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
$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