laravel 如何访问 Carbon 对象类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18371917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Carbon object type?
提问by Artisan
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']
?它仅用于格式化日期/时间目的。
采纳答案by netvision73
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 方法, read$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
回答by Sherif
you should use the public function toDateTimeString()
你应该使用公共函数 toDateTimeString()
echo $recordSet['created_at']->toDateTimeString();
回答by meen
public function getDates() {
return array();
}
Place this code in your model. This will disable date mutations.
将此代码放在您的模型中。这将禁用日期突变。