32
<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>

And I got the error message Parse error: syntax error, unexpected '<'
It look like the variable can`t read.
How to set the value in the for-loop?

2
  • try this @for ($i =$now; $i <= $last; $i--)
    – z3r0ck
    Jun 27, 2016 at 4:25
  • 1
    in a blade template, anything inside the @ directives (such as your @for()) will be treated as php, so there's no need to print it's value with {{ }}.
    – EricDS
    Jun 27, 2016 at 4:34

9 Answers 9

52

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

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>
4

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:

1

Change your view to:

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

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

You can write

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

It will resolve your error.

1

for days of the month you can do like so in laravel

<select class="form-control day-select">
   <option>Day</option>
      @foreach(range(1, 31) as $y)
        <option>{{$y}}</option>
      @endforeach
</select>

and for the years within a specified range say 1951 till date in descending order you can do

<select class="form-control">
   <option>Year</option>
   @foreach(range(date('Y')-16, date('Y')-70) as $y)
      <option>{{$y}}</option>
   @endforeach
 </select>
0

Hope this will help.

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

It will resolve you error, but would be nice if you pass your $last and $now variable from controller.

Thanks

0
    @foreach ($data as $item)
        @foreach ($order_item->where('order_id',$item->id) as $i)
        @endforeach
    @endforeach
0
  • You can simply use a forloop in blade file that's given below :

      <?php for($i = 0; $i < 6; $i ++){ ?>
         <span class="fa fa-star" style="color: #F2A057"></span>
      <?php } ?>
    

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.