PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] Can't access object values in Laravel blade loop

 January 28, 2022     arrays, laravel, laravel-5, object     No comments   

Issue

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 => []
  ]
  ....

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

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?


Solution

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

Remove the lasted @foreach



Answered By - Mohammad reza Golshahi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing