I'm getting undefined index errors on my blade when trying to take my array and plug the values into an html table structure.
I have the array looped and sending it to blade:
Controller
$result = array();
foreach($getItem as $Item){
$result[$Item->item_id][] = $Item;
}
//returning to blade but not included here
Dumped array:
array:26 [▼
11873 => array:2 [▼
0 => {#407 ▼
+"item_id": "11873"
+"item_name": "Title"
+"item_comment": "Item Title"
+"item_type": "2"
}
1 => {#408 ▼
+"item_id": "11873"
+"item_name": "Instruction"
+"item_comment": "Inst Comment"
+"item_type": "2"
}
]
Blade:
@foreach ($result as $id => $item)
<tr>
<td>{{ $item['item_id'] }}</td>
@if($item['item_name'] == "Title")
<td>{{ $item['item_comment'] }}</td>
@endif
<td>{{ $item['item_type'] }}</td>
</tr>
@endforeach
So with the dumped array structure, one issue is the fact that the 'item_type' should be at the high level with the id, and shouldn't be in each nested level. But other than that I get undefined index errors. Am I just looping incorrectly?
item_idis unique for each array element. Try changing$result[$Item->item_id][] = $Item;to$result[$Item->item_id] = $Item;item_id. Use your original array construction code and try the solution that I've posted as an answer.