Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j00rd's avatar
Level 2

Undefined Array Key When Reformatting Views - VideoGameAggregator

Hi, I'm following the Video Game Aggregator series, I am aware it was developed a while ago so not sure if this is what is causing my problems or if there is something I'm missing.

I am following Episode 10 to refactor the views and remove some of the logic from there. I'm hitting an error with the first collection that is made, I've followed the video and tried to figure it out but I keep hitting the error of Undefined Array Key when mapping over the records in the controller I'm not able to access the 'Cover' that is retrieved by the API. It works within the View but moving the logic across I'm not sure how to access it.

This is the PopularGames controller

 class PopularGames extends Component
{
    public $popularGames = [];

    public function load()
    {
        
        $before = Carbon::now()->subMonths(2)->timestamp;
        $after = Carbon::now()->addMonths(2)->timestamp;

        $popularGamesUnformatted = Cache::remember('popular-games', 100, function () use($before, $after) {
            
            return Http::withHeaders([                            /* Use HTTP client with headers of API tokens from .env */
                'Client-ID' => env('IGDB_KEY'),
                'Authorization' => env('IGDB_AUTH'),
            ])        
                ->withBody(                                                     /* Get the 12 highest rated games with their name and rating */
                    'fields name, cover, cover.url, first_release_date, total_rating_count, platforms.abbreviation, rating, slug;                                           
                    where platforms = (48,49,130,6)
                    & ( first_release_date >= '.$before.' 
                    & first_release_date <= '.$after.');
                    sort rating desc;
                    limit 12;',
                    'text/plain'
                )
                ->post('https://api.igdb.com/v4/games')->json();
        });

        $this->popularGames = $this->formatForView($popularGamesUnformatted);
     

    }

    public function render()
    {
        return view('livewire.popular-games');
    }

    private function formatForView($games)
    {
        return collect($games)->map(function ($game) {
            return collect($game)->merge([
                'coverImageUrl' => Str::replaceFirst('thumb', 'cover_big', $game['cover']['url']),
            ]);
        })->toArray();
    }
    
}

Trying to clean the code up so can call 'coverImageURL' from the view rather than the full logic however I get Undefined Array Key so am not really sure how to go about it.

Any Ideas?

Thank you

0 likes
1 reply
HMaxWilson's avatar

There is a high chance, like mine, that you're API request is returning an array which does not have cover data included

You could either try and catch an empty array object early in the body of the API request with

where cover != null

or check to ensure it is not null before you use it.

Note : You've either solved it or moved on by now, thought I'd comment in case anyone else was having the same issue we had

Please or to participate in this conversation.