Laravel Blade for loop not working with variable key: undefined index

  json, laravel, laravel-blade, php

In my blade file I am trying to access an array inside a for loop.

<ul>
    @for($i = 0; $i < count($json['products']); $i++)
        <li>
            {{ $json['products'][$i]['name'] }}
        </li>
    @endfor
</ul>

This gives me an error: Undefined index name but when I change $i variable to a hard coded 0 it does work.

<ul>
    @for($i = 0; $i < count($json['products']); $i++)
        <li>
            {{ $json['products'][0]['name'] }}
        </li>
    @endfor
</ul>

Displays the name like it should. When I dd on the $i variable it shows 0. What am I doing wrong and missing?

Source: Laravel

Leave a Reply