如何访问Carbon对象类型?

[英]How to access Carbon object type?


Given the following code:

给出以下代码:

$recordSets = Model::find(1)->get();

foreach ($recordSets as $recordSet) {
  dd($recordSet['created_at']);
}

I got this result.

我得到了这个结果。

object(Carbon\Carbon)[292]
  public 'date' => string '2013-08-21 17:05:19' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

I tried to access the 'date' using

我尝试使用“访问日期”

echo $recordSet['created_at']->date;

But I got an error:

但是我收到了一个错误:

Unknown getter 'date'

未知的吸气'日期'

How to access $recordSet['created_at']? It is just for formatting of date/time purpose.

如何访问$ recordSet ['created_at']?它仅用于格式化日期/时间目的。

3 个解决方案

#1


4  

Simply use $recordSet['created_at'].

只需使用$ recordSet ['created_at']。

Because of a __toString method in Carbon, read $recordSet['created_at'] will always return the date under string format.

由于Carbon中的__toString方法,读取$ recordSet ['created_at']将始终以字符串格式返回日期。

If you want to see which method you can use, see vendor/nesbot/carbon/Carbon/Carbon.php

如果您想查看可以使用的方法,请参阅vendor / nesbot / carbon / Carbon / Carbon.php

#2


25  

you should use the public function toDateTimeString()

你应该使用公共函数toDateTimeString()

echo $recordSet['created_at']->toDateTimeString();

#3


4  

  public function getDates() {
        return array();
  }

Place this code in your model. This will disable date mutations.

将此代码放在模型中。这将禁用日期突变。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/08/22/35971198babd33666ba4225adcb6444f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告