doulei8475 2016-11-14 17:01
浏览 332
已采纳

Laravel返回UTCDateTime而不是Carbon实例。 为什么?

I have a model that has the following in it:

protected $dates = ['created_at','modified_at','deleted_at', 'my_date'];

The same model is embedded in another document (using Mongo).

Now when I try to get the min and max dates in the model by doing:

 $minDate = $doctor->cases()->min('embedded_doc.my_date');
 $maxDate = $doctor->cases()->max('embedded_doc.my_date');

What I am getting are instances of UTCDateTime and not Carbon. The documentation states that if I list the date in the $dates array it would return Carbon instances. I am not clear. What am I missing?

  • 写回答

1条回答 默认 最新

  • dongpaozhi5734 2016-11-14 17:09
    关注

    It is happening because when you run min or max query it returns the single value, not an object of the Model.

    That's why it's not converted into Carbon.

    You can also write your query as:

    $doctor->cases()->orderBy('my_date')->select('my_date')->first()->my_date;
    

    This will give the Carbon object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64