0

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?

2 Answers 2

0

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)
        
0

Problem lies in your counter Decleration you should assign your counter outside of @foreach then increment counter. but now in your code you are doing increment and again making it $count = 1. Hope you will understand this logical mistake.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.