php - Wrong DateTime in created_at and updated_at fields in Laravel 5.2 -
i using jenssegers mongodb trying save created_at , updated_at in controller,
"updated_at" : isodate("1970-01-11t19:45:21.925z"), "created_at" : isodate("1970-01-11t19:45:21.925z")   and update also, wrong dates saving
in app.php
in aliases
'moloquent' => 'jenssegers\mongodb\eloquent\model',   in providers
'jenssegers\mongodb\auth\passwordresetserviceprovider',   in model
use moloquent;  class task extends moloquent{       //$fillables = []; }     please me in solving issue in advance!
actually - when using  jenssegers/laravel-mongodb package - created_at , updated_at attributes automatically set when saving new model object.  
however if still want manually set timestamps or else datetime field must convert datetime object (or carbon) mongodb\bson\utcdatetime.
so this:
$mymodel = new mymodel(); $mymodel->created_at = $mymodel->fromdatetime(new \datetime()); //...   and datetime attribute other created_at/updated_at:
class task extends model {     protected  $collection = 'tasks';      protected $duedate;      protected  $dates = ['duedate'];      /** mutator */     public function setduedateattribute($value)     {         /** @var \mongodb\bson\utcdatetime */         $this->attributes['duedate'] = $this->fromdatetime(             \datetime::createfromformat('d/m/y h:i', $value . '00:00'));     } }   jenssegers\mongodb\eloquent::fromdatetime() available model instance inherits parent model (see in github). method convert datetime storable utcdatetime object (which internal datetime mongo rep).

Comments
Post a Comment