Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach loop does not add counter

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
        
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading