0

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?

4
  • 1
    You are creating an unnecessary nesting level assuming that item_id is unique for each array element. Try changing $result[$Item->item_id][] = $Item; to $result[$Item->item_id] = $Item; Commented Aug 7, 2019 at 15:21
  • But when I do that it only shows one of the Item name and comment objects. That's fine for the item_type, but I still need all sets of item_name and item_comment Commented Aug 7, 2019 at 15:24
  • I see that you have multiple array elements with same item_id. Use your original array construction code and try the solution that I've posted as an answer. Commented Aug 7, 2019 at 15:30
  • Try the updated answer. Commented Aug 7, 2019 at 15:32

1 Answer 1

1

In @foreach ($result as $id => $item), $item is an array and you need to loop over it as well.

@foreach ($result as $id => $item)
    <tr>
        <td>{{ $id }}</td>
        @foreach($item as $subitem)
        @if($subitem['item_name'] == "Title")
            <td>{{ $subitem['item_comment'] }}</td>
        @endif
        <td>{{ $subitem['item_type'] }}</td>
        @endforeach
    </tr>
@endforeach
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this does work but the only issue now is that I still get two of the 'item_type'. Is there a way to only get that at the id level and not within the subitems?
You have two items for the single ID. Which one do you want to get?
They are tied to the id so they are always the same, I only get two because I have an inner join for name and comments
In that case, I suggest you to apply a limit at your query level, otherwise inconsistencies will be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.