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:
{{ 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:
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?
Laravel is using Carbon to manage datetime fields in database. By default, updated_at and created_at fields are automatically converted to a Carbon instance when retrieved, that's why you get the error on date() function.
To display the date, you have two options:
{{ $object->created_at }} // Will call toString() on object
// or
{{ $object->created_at->format('d M Y - H:i:s') }} // Format date to desired format
Carbon is a powerfull library to manage date. To learn more, see https://github.com/briannesbitt/Carbon
Hi MateusAzevedo
Thanks. I just realised I was being incredibly blonde trying to use date() to format the created_at value to the same format it's already in. I blame my lack of coffee today.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community