Albert
Albert

Reputation: 19

Looping through an array with a collection

I am using Laravel ViewModels to refactor the data returned from an API. The following is a function Im using to format the particular data

 public function similar()
    {
        $similarCollection = collect($this->similar)->map(function($movie){

            return collect($movie)->merge([
                'poster_path' => $movie['poster_path'] ? 
                  'https://image.tmdb.org/t/p/w500/'.$movie['poster_path']
                   : 'https://via.placeholder.com/500x750?text='.$movie['title'],
                'backdrop_path' =>  $movie['backdrop_path'] 
                   ?'https://image.tmdb.org/t/p/original/'.$movie['backdrop_path']
                    :'https://via.placeholder.com/500x750?text='.$movie['title'],

            ])->only([
                'poster_path','title','backdrop_path','backdrop_path'
            ]);

        });

         dd($similarCollection);

    }

The above function after dd() returns the following array

 Illuminate\Support\Collection {#394 ▼
  #items: array:20 [▼
    0 => Illuminate\Support\Collection {#357 ▼
      #items: array:3 [▼
        "title" => "The Fox and the Hound"
        "backdrop_path" => "https://image.tmdb.org/t/p/original//87uShMszqPxwA034GsskGdzJOgN.jpg"
        "poster_path" => "https://image.tmdb.org/t/p/w500//1382VHxqZDXu2t8i46zf4fP71JG.jpg"
      ]
    }
    1 => Illuminate\Support\Collection {#359 ▶}
    2 => Illuminate\Support\Collection {#358 ▶}
    3 => Illuminate\Support\Collection {#374 ▶}
    4 => Illuminate\Support\Collection {#371 ▶}
    5 => Illuminate\Support\Collection {#361 ▶}
    6 => Illuminate\Support\Collection {#362 ▶}
    7 => Illuminate\Support\Collection {#363 ▶}
    8 => Illuminate\Support\Collection {#366 ▶}
    9 => Illuminate\Support\Collection {#367 ▶}
    10 => Illuminate\Support\Collection {#354 ▶}
    11 => Illuminate\Support\Collection {#353 ▶}
    12 => Illuminate\Support\Collection {#360 ▶}
    13 => Illuminate\Support\Collection {#403 ▶}
    14 => Illuminate\Support\Collection {#401 ▶}
    15 => Illuminate\Support\Collection {#396 ▶}
    16 => Illuminate\Support\Collection {#400 ▶}
    17 => Illuminate\Support\Collection {#402 ▶}
    18 => Illuminate\Support\Collection {#368 ▶}
    19 => Illuminate\Support\Collection {#395 ▶}
  ]
}

In my blade view I have this code to loop through each item and display the backdrop_path as image

  @foreach ($similar['backdrop_path'] as $image)
            <div class="mt-8">
                <a href="#">
                    <img src="{{ $image }}" alt="Poster">
              </a>
           </div>

        @endforeach

The problem is it results in to "Trying to access array offset on value of type null" error message. How can I achieve desired results? Thanks.

Upvotes: 0

Views: 382

Answers (1)

A.A Noman
A.A Noman

Reputation: 5270

You have to use like below

@foreach ($similarCollection as $image)
    <div class="mt-8">
        <a href="#">
            <img src="{{ $image->backdrop_path }}" alt="Poster">
        </a>
    </div>
@endforeach

Upvotes: 1

Related Questions