Happy Laracon Week! All accounts are 60% off this week.

MattB's avatar
Level 2

How to increment for loop in blade

I don't know if Im going about this the wrong way but I wanted a section of a form that allows the user to click a button that adds in extra fields should they want to include extra information. I was planning on doing this in a for loop such as below, but didn't know how to increment the counter when someone clicks the plus icon at the bottom. Any advice please? Would this need to be done in jquery?

{{ $n = 3 }}
                    @for ($i = 0; $i < $n; $i++)
                    <div class="row px-3 pb-2">
                        <select class="form-control registerInputBoxes dropdownSelect" name="condition[{{$i}}]" id="illness[{{$i}}]" style="width: 100%;">
                            @foreach ($allConditions as $condition)
                                <option value="{{ $condition->id }}">{{ $condition->condition }}</option>
                            @endforeach
                        </select>
                        <div class="col-12">
                            <p class="registerText mb-0">Time since diagosis</p>    
                        </div>
                        <div class="col-4">
                            <label for="years" class="registerText pl-2">Years</label>
                            <input name="years" type="text" class="form-control w-25" style="float:left">
                        </div>
                        <div class="col-4">
                            <label for="years" class="registerText pl-2">Months</label>
                            <input name="months" type="text" class="form-control w-25" style="float:left"> 
                        </div>
                        <div class="col-4">
                            <label for="years" class="registerText pl-2">Days</label>
                            <input name="days" type="text" class="form-control w-25" style="float:left"> 
                        </div>
                    </div>
                    @endfor
            </div>
            <div class="text-right col-12">
                <a href="#"><span class="plus" id="addAnotherIllness">+</span></a>
            </div>
0 likes
4 replies
Sinnbeck's avatar

Are you using any kind of javascript? Cause unless you want the page to reload, you need javascript.

MattB's avatar
Level 2

Fair point. I am using jquery but that isn't my strong suit. What would be the best way to increment that counter then, please?

Snapey's avatar

hands down the best way is with Livewire, but there is a learning stage.

Better to learn that though than learning jquery at this point (IMO)

SarwarAhmed's avatar

@Mattb Increment with PHP. But you can take Snapey's suggestion.

@php $n = 3 @endphp
    
@for ($i = 0; $i < 3; $i++)

    @while($i != $n+1)
    {{ $i++ }}
    @endwhile
@enfor

Output: 0 1 2 3
2 likes

Please or to participate in this conversation.