Foreach loop does not add counter

Issue

I have this custom foreach loop at Blade:

<div id="dynamic_field">
    @foreach($niloufars as $niloufar)
        @php 
            $counter = 1;
        @endphp 
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <span class="text-danger force-required"></span>
                    <label for="niloufar_name">Title</label>
                    <input type="text"
                        class="form-control"
                        name="niloufar_name_{{ $counter }}" value="{{ $niloufar->name }}">
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <span class="text-danger force-required"></span>
                    <label for="niloufar_link">Link</label>
                    <input type="text"
                        class="form-control"
                        name="niloufar_link_{{ $counter }}" value="{{ $niloufar->link }}">
                </div>
            </div>
        </div>  
        @php
            $counter = $counter + 1;
        @endphp
    @endforeach
</div>

So as you can see I have set a $counter variable set to 1 at the 1st iteration and then I add 1 to it so that for the next iteration it will be 2.

Therefore I can set unique names for form fields in order for further process.

But the problem with this is that, the variable is just set to 1 the whole time:

     <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <span class="text-danger force-required"></span>
                <label for="niloufar_name">Title</label>
                <input type="text"
                    class="form-control"
                    name="niloufar_name_1" value="">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <span class="text-danger force-required"></span>
                <label for="niloufar_link">Link</label>
                <input type="text"
                    class="form-control"
                    name="niloufar_link_1" value="">
            </div>
        </div>
    </div>  
                         
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <span class="text-danger force-required"></span>
                <label for="niloufar_name">Title</label>
                <input type="text"
                    class="form-control"
                    name="niloufar_name_1" value="">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <span class="text-danger force-required"></span>
                <label for="niloufar_link">Link</label>
                <input type="text"
                    class="form-control"
                    name="niloufar_link_1" value="">
            </div>
        </div>
    </div>  

So what’s going wrong here? How can I show current iteration properly for each form field name?

Solution

Move the setting of the counter before the foreach (as it is, you are setting it to 1 in the beginning of every loop):

@php 
    $counter = 1;
@endphp 
@foreach($niloufars as $niloufar)
        

Answered By – Ross_102

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published