Fan
Fan

Reputation: 1180

How to set value in for-loop on laravel blade

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

Upvotes: 33

Views: 192098

Answers (9)

Mahadi  Hassan Babu
Mahadi Hassan Babu

Reputation: 511

  • 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 } ?>
    

Upvotes: 0

Akhilendra Singh
Akhilendra Singh

Reputation: 769

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>

Upvotes: 8

Mr. Sukhdev
Mr. Sukhdev

Reputation: 21

    @foreach ($data as $item)
        @foreach ($order_item->where('order_id',$item->id) as $i)
        @endforeach
    @endforeach

Upvotes: 0

bigdevlarry
bigdevlarry

Reputation: 245

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>

Upvotes: 1

Minhaj Mimo
Minhaj Mimo

Reputation: 836

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>

Upvotes: 52

Xerif
Xerif

Reputation: 140

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:

Upvotes: 4

Jahid Mahmud
Jahid Mahmud

Reputation: 1136

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.

Upvotes: 1

Osama Sayed
Osama Sayed

Reputation: 2023

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>

Upvotes: 1

mizan3008
mizan3008

Reputation: 379

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

Upvotes: 0

Related Questions