Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Xron's avatar
Level 1

Cannot use object of type Carbon as array, error

Hi,

I'm trying to fetch the last 7 dates from the current date, and output those in my blade view, this is what I have so far -

$start = Carbon::now()->subDays(7); for ($i = 0 ; $i <= 7; $i++) { $dates[] = $start->copy()->addDays($i); }

@foreach($dates as $date) {{ $date['date'] }} @endforeach

The following error is thrown in the view - Cannot use object of type Carbon\Carbon as array

0 likes
4 replies
ahuggins's avatar

This line: @foreach($dates as $date) {{ $date['date'] }} @endforeach

Might try like this: @foreach($dates as $date) {{ $date->date }} @endforeach

I think the $date variable is an instance of Carbon, which apparently can not be accessed like an array...so try accessing it like an object.

Xron's avatar
Level 1

I did try that but then I get - Unknown getter 'date'

Which is odd because if I die dump the variable $dates then I get an array with 8 objects shown like this -

array:8 [▼ 0 => Carbon {#187 ▼ +"date": "2016-08-07 16:42:06.000000" +"timezone_type": 3 +"timezone": "UTC" } 1 => Carbon {#186

Xron's avatar
Level 1

Thank you I was just a little confused because the array was showing using the key date however I stitched the date together using day, month, and year!

thanks

Please or to participate in this conversation.