vendredi 4 mars 2016

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1TYK39t
via IFTTT

Aucun commentaire:

Enregistrer un commentaire