Home > Software design >  Can't access object values in Laravel blade loop
Can't access object values in Laravel blade loop

Time:10-21

I'm facing strange behavior when trying to access object values in Laravel blade.

My data looks like the following:

<?php


array:11 [▼
  "Monday, 18.10.2021" => array:6 [▶
    1 => array:1 [▶
      0 => {#1532 ▶
         "id": 252
         "status": 1
         "heading": "Online Event für Test #1"
         "customer": "Test #1"
         "event_type_id": 12
         "slug": "event_11111"
         "datetime": "2021-10-18 10:00:00"
         "name": "Online Event"
         "duration": 3
      }
    ]
    2 => array:1 [▶
      0 => {#1501 ▶
         "id": 252
         "status": 1
         "heading": "Online Event für Test #1"
         "customer": "Test #1"
         "event_type_id": 12
         "slug": "event_11111"
         "datetime": "2021-10-18 10:00:00"
         "name": "Online Event"
         "duration": 3
      }
    ]
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  "Tuesday, 19.10.2021" => array:6 [▶
    1 => []
    2 => []
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  "Wednesday, 20.10.2021" => array:6 [▶
    1 => []
    2 => []
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  "Thursday, 21.10.2021" => array:6 [▶
    1 => []
    2 => []
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  "Friday, 22.10.2021" => array:6 [▶
    1 => array:1 [▶
      0 => {#1637 ▶
         "id": 253
         "status": 1
         "heading": "Online Event für Test #2"
         "customer": "Test #2"
         "event_type_id": 12
         "slug": "event_22222"
         "datetime": "2021-10-22 14:00:00"
         "name": "Online Event"
         "duration": 5
      }
    ]
    2 => array:1 [▶
      0 => {#1615 ▶
         "id": 253
         "status": 1
         "heading": "Online Event für Test #2"
         "customer": "Test #2"
         "event_type_id": 12
         "slug": "event_22222"
         "datetime": "2021-10-22 14:00:00"
         "name": "Online Event"
         "duration": 5
      }
    ]
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  "Saturday, 23.10.2021" => array:6 [▶
    1 => []
    2 => []
    3 => []
    5 => []
    6 => []
    31 => []
  ]
  ....
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

And my blade file like this:

<?php

             @foreach($overview_pivot as $datetime => $users)
                <tr>
                  <td>{{ $datetime }}</td>
                  @foreach($users as $user_id => $user_events_array)
                  <td>
                    @foreach($user_events_array as $events)
                      <ul class="list-group list-group-flush" data-toggle="checklist">
                        @foreach($events as $event)

                           //Works fine
                           {{ $event  }} 

                           //Error
                           {{ $event->id  }} 
                        
                        @endforeach
                      </ul>
                    @endforeach

                  </td>
                  @endforeach
                </tr>

              @endforeach
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Problem:

When I try accessing the object {{ $event }} everything works fine, but when I try to echo object values (e.g. {{ $event->id }}). I get the error "Trying to get property 'id' of non-object".

Any idea what's wrong with my code?

CodePudding user response:

@foreach($events as $event) is mistake.

Remove the lasted @foreach

  • Related