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.

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统