我使用的是监控程序mongodb,我试图在控制器中保存created_at和updated_at,
"updated_at" : ISODate("1970-01-11T19:45:21.925Z"),
"created_at" : ISODate("1970-01-11T19:45:21.925Z")即使是更新,错误的日期也在保存。
在我的app.php里
化名
'Moloquent' => 'Jenssegers\Mongodb\Eloquent\Model',在提供者中
'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',在我的模型里
use Moloquent;
class Task extends Moloquent{
//$fillables = [];
}

请提前帮助我解决这个问题,谢谢!
发布于 2016-04-25 05:30:29
实际上-在使用jenssegers/Laravel-MongoDB包时,保存新模型对象时会自动设置、created_at、和updated_at属性。
但是,如果仍然希望手动设置时间戳或任何其他日期时间字段,则必须将datetime对象(或碳)转换为MongoDB\BSON\UTCDateTime。
所以应该是这样的:
$myModel = new MyModel();
$myModel->created_at = $myModel->fromDateTime(new \DateTime());
//...对于创建的_at/updated_at以外的另一个datetime属性:
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()可以从任何模型实例中获得,因为它是从父模型继承的(参见github)。此方法将DateTime转换为可存储的UTCDateTime对象(这是内部datetime mongo代表)。
https://stackoverflow.com/questions/36832359
复制相似问题