katie hudson
katie hudson

Reputation: 2893

Laravel 5 Blade - strange occurence in foreach

I have a collection which I am displaying below as an array to make it easier to view.

array:3 [▼
  0 => array:7 [▼
    "id" => 6
    "name" => "food"
    "value" => "T1 M1"
  ]
  1 => array:7 [▼
    "id" => 7
    "name" => "drink"
    "value" => ""
  ]
  2 => array:7 [▼
    "id" => 8
    "name" => "drink"
    "value" => "A1 P1"
  ]
]

Within my view I am doing something like the following

<div class="col-md-3 noPadding">
    @foreach($party->partyOptions as $id => $data)
        @if ($data->name === 'food')
            <div class='col-md-12'>
                <label>Number of food {{ $id + 1 }}:</label>
            </div>
        @endif
    @endforeach
</div>

<div class="col-md-3 noPadding">
    @foreach($party->partyOptions as $id => $data)
        @if ($data->name === 'drink')
            <div class='col-md-12'>
                <label>Number of drink {{ $id + 1 }}:</label>
            </div>
        @endif
    @endforeach
</div>

I do different loops for the types because I am doing some additional stuff which I have not shown above. Anyways, with the above, I would expect the first div to display

Number of food 1

And the second div to display

Number of drink 1
Number of drink 2

This is not the case however. Instead, it continues the id count, so what I get outputted is

Number of food 1

Number of drink 2
Number of drink 3

If I have more names I am searching for, the number continues.

Why would this be happening? Shouldnt the id get reset for each loop?

Any information regarding this appreciated.

Thanks

Upvotes: 0

Views: 71

Answers (2)

Kocik
Kocik

Reputation: 504

Loop behavior is perfectly normal. Your$id is index in the array you are iterating. See documantation: https://secure.php.net/manual/en/control-structures.foreach.php

So in your array you have "drink" under indexes 1 and 2. You need different approach - you need separate variable that you increment inside if block:

<div class="col-md-3 noPadding">
{{$i = 0}}
    @foreach($party->partyOptions as $id => $data)
        @if ($data->name === 'drink')
            {{$i ++}}
            <div class='col-md-12'>
                <label>Number of drink {{ $i }}:</label>
            </div>
        @endif
    @endforeach
</div>

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 220136

You're using the index of the collection, not of the iteration.

To get the index you want, create a new collection that is filtered to just what you want:

<div class="col-md-3 noPadding">
    @foreach($party->partyOptions->where('name', 'food')->values() as $id => $data)
        <div class="col-md-12">
            <label>Number of food {{ $id + 1 }}:</label>
        </div>
    @endforeach
</div>

<div class="col-md-3 noPadding">
    @foreach($party->partyOptions->where('name', 'drink')->values() as $id => $data)
        <div class="col-md-12">
            <label>Number of drink {{ $id + 1 }}:</label>
        </div>
    @endforeach
</div>

Upvotes: 2

Related Questions