Ricki Moore
Ricki Moore

Reputation: 1223

@foreach not looping array as expected

I'm trying to loop through an array provided from facebook graph api. The initial array holds two fields (data, and paging). The data field is also an array filled with 50+ items.

    array:2 [▼
  "data" => array:77 [▶]
  "paging" => array:2 [▶]
]

and opened:

    array:2 [▼
  "data" => array:77 [▼
    0 => array:5 [▼
      "id" => "238123092879087er098234_28365"
      "picture" => "https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-0/s130x130/12235138_10we2133658asdasdfafsasf5816390956_3188793651778686071_n.jpg?oh=eb8f907a1e8df5efe99cb2b9fafa9c05&oe=56E5627B"
      "message" => """
        some new GIN available @ TasTToe!!\n
        \n
        MASCARO 9 GIN 40°\n
        Alkkemist GIN  \n
        ATOMIC GIN 40° FROM AtomicDistillers\n
        Elephant Gin\n
        PLATU LONDON DRY GIN 39°
        """
      "link" => "https://www.facebook.com/2381232313223159570563/photos/a.238553749527504.54095.238123159570563/1036585816390956/?type=3"
      "full_picture" => "https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-9/12235138_1032365824332445816390956_318234438793651778686071_n.jpg?oh=21fcf828fac0dd49e125b13028d57bfb&oe=56EB6A10"
    ]

While using the foreach of the facebook results I try to loop through the data field for all the contents yet I am returned with error:

Trying to get property of non-object (View: /var/www/web/elephantegin/htdocs/resources/views/socialApps/facebook.blade.php)

This is the code I used:

 @foreach($results->data as $result)
    <p>poops</p>
 @endforeach

and alternatively this:

@foreach($results as $result)
     @foreach($result->data as $data)
       <p>help</p>
       @endforeach
        @endforeach

I've done this times before with no problem. In my controller use json_decode on the results before I push them to the view. How can i call this foreach properly?

Upvotes: 1

Views: 127

Answers (1)

skad0
skad0

Reputation: 163

If I get you right, then you have an array, but trying to get property. That is how it should be:

@foreach($results['data'] as $result)

Upvotes: 3

Related Questions