Alan Spurlock
Alan Spurlock

Reputation: 107

Getting values from an array throws ErrorException "Trying to get property of non-object"

I am trying to display the keys for each event in an array.

This is the var_dump

PAMI\Message\ResponseResponseMessage (object) [Object ID #374][9 properties]
events: (array) [29 elements]
    0:  PAMI\Message\EventPeerEntryEvent (object) [Object ID #381][6 properties]
            rawContent:protected: (string) Event: PeerEntry
            ActionID: 1517499612.3137
            Channeltype: SIP
            ObjectName: 100
            ChanObjectType: peer
            IPaddress: -none-
            IPport: 0
            Dynamic: yes
            AutoForcerport: no
            Forcerport: yes
            AutoComedia: no
            Comedia: yes
            VideoSupport: no
            TextSupport: no
            ACL: yes
            Status: UNKNOWN
            RealtimeDevice: no
            Description: 
        channelVariables:protected: (array) [1 element]
            default: (array) [0 elements]
        lines: (array) [0 elements]
        variables: (array) [0 elements]
        keys: (array) [18 elements]
            event: (string) PeerEntry
            actionid: (string) 1517499612.3137
            channeltype: (string) SIP
            objectname: (string) 100
            chanobjecttype: (string) peer
            ipaddress: (string) -none-
            ipport: (string) 0
            dynamic: (string) yes
            autoforcerport: (string) no
            forcerport: (string) yes
            autocomedia: (string) no
            comedia: (string) yes
            videosupport: (string) no
            textsupport: (string) no
            acl: (string) yes
            status: (string) UNKNOWN
            realtimedevice: (string) no
            description: (string) 
            createdDate: (integer) 1517499612

    1: etc........

    2: etc........

and this is in my blade.php

       @foreach($data->events as $peer)
            <tr>
                <td>{{ $peer->keys->ipaddress }}</td>
                <td></td>
                <td></td>
            </tr>
        @endforeach

I keep getting the error

"Trying to get property of non-object"

I know what the error means, but I don't know what I am doing wrong.

If I just do a var_dump on the $peer, I can see all the keys, but I just can not access them.

Any help would be appreciated!

Edit** So i did a var_dump like so

   @foreach($data->events as $peer)
        <tr>
            <td>
            @if($peer->keys)
                {{ var_dump($peer->keys) }}
            @endif
            </td>
            <td></td>
            <td></td>
        </tr>
    @endforeach

This is the output of dd https://pastebin.com/0Wv4nehp

The problem now, is that I can only use "event" or "actionid" {{ $peer->keys["event"] }}

if i try to use anything else in that array... {{ $peer->keys["status"] }}

I get this Undefined index: status

I at a loss for words here.

Upvotes: 2

Views: 116

Answers (3)

Sohel0415
Sohel0415

Reputation: 9853

Use [] to access array element instead of -> which used to access object property.

@foreach($data->events as $peer)
        <tr>
            <td>{{ $peer->keys["ipaddress"] }}</td>
            <td></td>
            <td></td>
        </tr>
@endforeach

If there any possibility of missing element, then you can do this-

@foreach($data->events as $peer)
        <tr>
             @foreach($peer->keys as $key=>$value)
               <td>{{ $value }}</td>
             @endforeach
        </tr>
@endforeach

Upvotes: 2

Khachik
Khachik

Reputation: 9

In your example I see each event have many keys You are trying to display all events and all keys for each events

@foreach($data->events as $peer)
    <tr>
        <td>
             @foreach($peer->keys as $key)
                 {{ $key->ipaddress }} ,
             @endforeach
        </td>
        <td></td>
        <td></td>
    </tr>
@endforeach

This code should display all events and ip addresses separated by comma

Upvotes: 0

Prince Lionel N&#39;zi
Prince Lionel N&#39;zi

Reputation: 2588

Try to use

   @foreach($data->events as $peer)
        <tr>
            <td>
              @if($peer->keys)
                  {{ $peer->keys->ipaddress }}
              @endif
            </td>
            <td></td>
            <td></td>
        </tr>
    @endforeach

Upvotes: 0

Related Questions