Whisou138
Whisou138

Reputation: 481

accessing multidimensional array in laravel blade

I'm trying to access the 'cat' value in my array below, coming from my controller.

If I dump $tempCategories it shows the array correctly but my html is showing nothing for some reason.

Am I not accessing the element correctly?

I expect to see

Wood
Metal

controller.php

  $tempCategories = array(
        0 => array(
            'cat' => 'Wood'
        ),
        1 => array(
            'cat' => 'Metal'
        ),
    );

blade.php

@foreach($tempCategories as $cat)
<h5>{{$cat->cat}}</h5>
@endforeach

Upvotes: 0

Views: 2631

Answers (2)

Shimazakhi
Shimazakhi

Reputation: 39

If you want to access it with arrow operator - convert your array to object or collection first (in your controller)

$object = (object) $array;

Or

$collection = collect($array);

Upvotes: 0

Felippe Duarte
Felippe Duarte

Reputation: 15131

You are trying to access an array as object

Replace

<h5>{{$cat->cat}}</h5>

With

<h5>{{$cat['cat']}}</h5>

Upvotes: 3

Related Questions