How to set value in for-loop on laravel blade
The error message Parse error: syntax error, unexpected '<' is a classic symptom of trying to execute PHP logic directly inside Blade templates without respecting their distinct compilation layers. In your specific example, the issue stems from placing raw PHP variable assignments ($last, $now) immediately before the @for directive within the same line or context where Blade expects its own syntax tags.
Blade does not simply pass through everything you write; it compiles specific directives like @php into raw PHP, but it does not allow bare PHP statements to sit loose in the template unless explicitly wrapped. When you write:
{{ $last= date('Y')-120 }}
{{ $now = date('Y') }}
You are attempting to echo these assignments. The {{ }} syntax is designed for outputting variables, not executing statements that return void or modify scope in a way intended for logic flow. Consequently, the parser gets confused about where the template context ends and raw PHP begins, leading to the unexpected < error when it encounters the subsequent @for tag or the closing of the select element.
To resolve this, you must encapsulate your logic calculations within a @php block. This tells the Blade compiler: "Treat everything inside here as standard PHP, compile it, and do not attempt to echo it unless explicitly told."
Here is the corrected implementation:
<select id="year" name="year" class="form-control">
@php
$now = (int) date('Y');
$last = (int) (date('Y') - 120);
@endphp
@foreach(range($now, $last, -1) as $i)
<option value="{{ $i }}">{{ $i }}</option>
@endforeach
</select>
Why Switch to range?
While the @for directive works perfectly fine for this use case, utilizing PHP's native range() function inside a Blade @foreach loop often yields cleaner code and better performance. The @for directive in Laravel specifically handles numeric increments or decrements. Your original syntax $i-- is valid, but mixing raw PHP logic outside of @php tags is the root cause of the failure.
By moving the calculation into @php, we ensure that $last and $now are fully resolved before the loop starts. We cast them to (int) because date('Y') returns a string in older PHP versions, and Blade's iteration logic works most predictably with integers to avoid potential string comparison quirks in the loop condition.
Handling the Loop Direction
Your original code used $i <= $last with a decrement operator $i--. This implies you wanted to count backwards from the current year down to 120 years ago. In Blade, @for ($i = $start; $i >= $end; $i--) is the standard syntax for descending loops. However, if you prefer the more concise range() approach shown above, it automatically handles the step direction.
If you stick with @for, ensure your condition matches the decrement operator:
@php
$now = (int) date('Y');
$last = (int) (date('Y') - 120);
@endphp
@if($now >= $last) {{-- Optional safety check --}}
@for ($i = $now; $i >= $last; $i--)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
@endif
Common Pitfalls to Avoid
A frequent mistake developers make when mixing PHP and Blade is assuming that variables set in a line above a directive carry over if the intermediate lines use {{ }}. In Blade, every {{ }} block is an echo statement. It compiles to $output = $content; essentially. If you try to assign a value inside {{ }}, you are technically echoing the result of that assignment (which is usually null or causing a parse error if the syntax isn't perfect), not storing it for later use in the same block's scope.
Always isolate logic, calculations, and variable assignments inside @php ... @endphp blocks when building complex forms like this year selector. This keeps your template readable and prevents the parser from getting lost between echo statements and control structures.
For further reading on Blade syntax and best practices for mixing PHP logic with templates, you might find additional context in resources such as https://laravelcompany.com.
