Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value in for-loop on laravel blade

Tags:

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

like image 240
Fan Avatar asked Jun 27 '16 04:06

Fan


People also ask

Which is the correct looping syntax in term of blade engine?

The blade templating engine provides loops such as @for, @endfor, @foreach, @endforeach, @while, and @endwhile directives. These directives are used to create the php loop equivalent statements.

What is __ In Laravel blade?

Laravel later introduced a great helper function __() which could be used for JSON based translations. For instance, in your blade files, {{ __('The Web Tier') }} Whereas “The Web Tier” is added in a JSON file inside of resources/lang directory i.e {locale}.json likeso, {


1 Answers

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> 
like image 155
Minhaj Mimo Avatar answered Sep 22 '22 09:09

Minhaj Mimo