How to set value in for-loop on laravel blade

The best way to do this is to use range() and foreach()

Example:

@foreach(range(date('Y')-5, date('Y')) as $y)
    {{$y}}
@endforeach

Result

2014 2015 2016 2017 2018 2019 

Documentation:

  • range() : https://www.php.net/manual/en/function.range.php

using @for ($i = $now; $i <= $last; $i--) didn't work for me so I had to use incrementing count.

<div class="form-group">
  <label for="task" class="col-sm-1 control-label">Text</label>
     @for ($i = 0; $i < $count; $i++)
       <div class="col-sm-12">
           <input type="text" name="text[{{ $i }}]" id="text[{{ $i }}]" class="form-control">
      </div>
     @endfor
</div>

Basically {{ $last= date('Y')-120 }} in this part you are showing the value but you need to assign the value. So assign like this :

<?php $last= date('Y')-120; ?>

Same thing goes for the for loop too.Just compare the value. Do not put it in blade syntax.

<select id="year" name="year" class="form-control ">
    {{ $last= date('Y')-120 }}
    {{ $now = date('Y') }}

    @for ($i = $now; $i >= $last; $i--)
        <option value="{{ $i }}">{{ $i }}</option>
    @endfor
</select>