Home > Back-end >  I have a collection of objects in laravel. When I try to access it in blade it shows "Attempt t
I have a collection of objects in laravel. When I try to access it in blade it shows "Attempt t

Time:07-16

^ Illuminate\Support\Collection {#1471 ▼
  #items: array:9 [▼
    0 => {#1490 ▼
       "year_month": "2022-01"
    }
    1 => {#1452 ▼
       "year_month": "2022-02"
    }
    2 => {#526 ▼
       "year_month": "2022-03"
    }
    3 => {#1453 ▼
       "year_month": "2022-04"
    }
    4 => {#1457 ▼
       "year_month": "2022-05"
    }
    5 => {#1458 ▼
       "year_month": "2022-06"
    }
    6 => {#1461 ▼
       "year_month": "2022-07"
    }
    7 => {#1463 ▼
       "year_month": "2022-08"
    }
    8 => {#1460 ▼
       "year_month": "2022-09"
    }
  ]
  #escapeWhenCastingToString: false
}

This is my collection. And I try to access it in Blade as

@if(!empty($monthYears))
    <div >
        @foreach($monthYears as $monthYear)
            <div wire:click="changePeriod('{{$monthYear->year_month}}')" >
                {{date("m/y", strtotime($monthYear->year_month))}}
            </div>
        @endforeach
   </div>
@endif

On the first load it loads without any issue. But when I click on any events of livewire components it shows "Attempt to read property "year_month" on array".

Here's my livewire component

public Collection $monthYears;

public function mount()
{
    $this->monthYears = DB::table('table_name')->orderBy('year_month', 'asc')->groupBy('year_month')->get('year_month');
}

CodePudding user response:

Livewire does not hydrate stdClass properties. They will be converted to array structure.

Note the supported types.

  • Related