laravel 无法在视图中显示 created_at 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28290426/
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
Trouble displaying created_at value in view
提问by David Ross
I'm trying to display the created_at column in one of my views and I'm not having any luck. When I try using the below:
我试图在我的一个视图中显示 created_at 列,但我没有任何运气。当我尝试使用以下内容时:
{{ date('d M Y - H:i:s', $object->created_at) }}
{{ date('d M Y - H:i:s', $object->created_at) }}
I get the following error: date() expects parameter 2 to be long, object given. So I use dd() on $object->created_at and it gives me this:
我收到以下错误:date() 期望参数 2 很长,给定的对象。所以我在 $object->created_at 上使用 dd() ,它给了我这个:
object(Carbon\Carbon)#555 (3) { ["date"]=> string(26) "2015-01-22 14:36:37.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "Africa/Johannesburg" }
object(Carbon\Carbon)#555 (3) { ["date"]=> string(26) "2015-01-22 14:36:37.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "Africa/Johannesburg" }
So naturally I tried accessing the date with a foreach loop which didn't work and I also tried $object->created_at->date which gave me an error "Unknown getter 'date'" How do I access the date from created_at?
所以很自然地,我尝试使用无效的 foreach 循环访问日期,我还尝试了 $object->created_at->date 这给了我一个错误“未知的 getter 'date'” 如何从 created_at 访问日期?
回答by Edgar Orozco
Laravel uses Carbon. Then try this:
Laravel 使用碳。然后试试这个:
{{ date('d M Y - H:i:s', $object->created_at->timestamp) }}
Or this is the same without date function
或者这与没有日期功能的情况相同
{{ $object->created_at->format('d M Y - H:i:s') }}
Or use any method documented in the Carbon API
或者使用Carbon API 中记录的任何方法
回答by Hasan Hafiz Pasha
This is another method -
这是另一种方法——
{{ date('F d, Y', strtotime($list->created_at)) }}