4 years ago

foreach on with toarray

Posted 4 years ago by FutureWeb

Hiya LaraPeeps,

I have a collection with relationships ->toArray() that looks something like this:

 $phone = Phone::with('reviews','specifications','retailers')->first()->toArray();

Now if there is only one review it breaks my foreach in the view why is this?

  @foreach($phone['reviews'] as $r)
            // html

I don't want to have to do a condition as it would mean repeating loads of html

@if(count($phone['reviews'] >=0)
   // print out single review
    // loop through
    @foreach($phone['reviews'] as $r)
      // html 

seems a little silly any ideas why the foreach won't work with only 1 element in the $phone['reviews'] array?

thanks in advance

Please sign in or create an account to participate in this conversation.