Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robtcallahan's avatar

syntax error, unexpected token "endforeach"

Hi folks,

I'm getting this error on the following code

@foreach ($days as $day => $meetings)
    @if (!$loop->first)
        </div>
    @endif
@endforeach

However, if I put raw HTML after the @foreach, all is well. Why is this? FWIW, I'm finding Laravel to be very picky at least when it comes to views.

I appreciate the help! Rob

0 likes
16 replies
robtcallahan's avatar

To get around this error, I did the following, including adding the { after the @if. WTH?

@foreach ($days as $day => $meetings)
    <pre></pre>
    @if (!$loop->first) {
        </div>
    @endif
@endforeach
Snapey's avatar

@robtcallahan so now you have an extra { in your output

I don't really see how your code produces valid html so you have clearly simplified the code in the question for our benefit so we are not seeing the real issue.

robtcallahan's avatar

Thanks for trying help me out. For whatever reason, it's not causing a stack trace anymore with @php after the @foreach. I also simplified by using @if instead of @php as follows:

        @foreach ($days as $day => $meetings)
            @if (!$loop->first)
                @php
                    echo '</div></div>';
                    $row++;
                    $column = 1;
                    $num_column_lines = 0;
                @endphp
            @endif
            <div class="row"><h1 class="meeting-day">{{ strtoupper($day) }} MEETINGS</h1><div class="column">
        ....  more code here
         @endforeach
Andi1982's avatar

Maybe have a look at the compiled view in the cache? For me sometimes helped a lot, but always was hard to find the correct file. But you can see there, which php-code will be generated.

1 like
robtcallahan's avatar

@Andi1982 yes, I found that does help. It definitely showed errors on this issue. Not sure why it went away, but I suspect it'll crop up again. I'll look there first and do some troubleshooting before posting next time. Thanks!

robtcallahan's avatar

It's happening again. Admittedly, I'm new to Laravel but I do not see anything wrong. I'm showing the complete foreach block here. The error is occurring on the @endforeach at the bottom: syntax error, unexpected token "endforeach", expecting end of file

@foreach ($days as $day => $meetings)
    @if (!$loop->first)
        </div></div>
        @php
            $row++;
            $column = 1;
            $num_column_lines = 0;
        @endphp
    @endif
    <div class="row">
         <h1 class="meeting-day">{{ strtoupper($day) }} MEETINGS</h1>
         <div class="column">

    @php
        if (need_new_column($num_column_lines)) {
            if (is_last_column($column)) {
                echo new_row();
                $row++;
                $column = 1;
            } else {
                echo new_column();
                $column++;
            }
            $num_column_lines = 0;
        }
        $num_column_lines++;
    @endphp

    @foreach ($meetings as $meeting)
        @php
            $num_meeting_lines = get_num_meeting_lines($meeting);
            if (need_new_column($num_column_lines + $num_meeting_lines)) {
                if (is_last_column($column)) {
                    $row++;
                    $column = 1;
                    $num_column_lines = 0;
                    echo new_day($day);
                } else {
                    echo new_column();
                    $column++;
                }
                $num_column_lines = 0;
            }
            $num_column_lines += $num_meeting_lines;
        @endphp
        @include('meeting-day', compact('meeting'))
    @endforeach
    </div>
@endforeach
Snapey's avatar

whats in your meeting-day include file?

robtcallahan's avatar

@Snapey

<style>
    .meeting-day-time-region {
        font-size: 14px;
        font-weight: bold;
        text-decoration: underline;
        /*border-bottom: 2px solid black;*/
        margin-bottom: 1px;
    }
    .meeting-day-name {
        font-weight: bold;
    }
</style>

<div class="">
    <div class="meeting-day-time-region">
        {{ $meeting->time_formatted }} - {{ $meeting->regions_formatted }}
    </div>
    <div class="meeting-day-name">
        {{ $meeting->name }}
    </div>
    <div>
        @if ($meeting->location && $meeting->location !== $meeting->name)
            {{ $meeting->location }},
        @endif
        {{ $meeting->address }}
    </div>
    <div class="types">
        ({{ implode(', ', $meeting->types) }})
    </div>
</div>
robtcallahan's avatar
robtcallahan
OP
Best Answer
Level 1

I found the source of the problem, even though it shouldn’t be a problem. I looked at the cache file that Laravel produces which is an expansion of all the @ functions like if, foreach, etc. Some were not expanded. Those that weren’t, were followed by a PHP statement in the form of @php( line_of_php_code_here ). While this is a documented Laravel view statement, it doesn’t work. I substituted that for <?php line_of_php_code_here; ?> and all is well.

Can anyone tell me why @phhp() statements do not work as advertised?

robtcallahan's avatar

Ok, so here's a snippet of code.

        @foreach ($regions as $region => $days)
            @if (!$loop->first)
                <?php list ($row, $column, $num_column_lines) = check_new_row_column($row, $column, $num_column_lines); ?>
            @else
                <div class="row"><div class="column"><div class="region"><h1>{{ $region }}</h1></div>
            @endif
            @php($num_column_lines++)
            @foreach ($days as $day => $meetings)

This causes a stack dump to occur at the last line with the error message: syntax error, unexpected token "endforeach", expecting "elseif" or "else" or "endif"

I checked the resulting cache file which looks as follows:

    <?php elseif($group_by === 'region-day'): ?>
        <?php $__currentLoopData = $regions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $region => $days): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php if(!$loop->first): ?>
                <?php list ($row, $column, $num_column_lines) = check_new_row_column($row, $column, $num_column_lines); ?>
            <?php else: ?>
                <div class="row"><div class="column"><div class="region"><h1><?php echo e($region); ?></h1></div>
            <?php endif; ?>
            <?php($num_column_lines++)
            @foreach ($days as $day => $meetings)

You can see that the @php($num_column_lines++) statement is not converted properly as it is missing the ?> at the end. Laravel version is 10.18.0 PHP version is 8.2.7

JussiMannisto's avatar

@robtcallahan I tested the single statement directive it and it works. I also tested your code and it compiles properly when I close the last @foreach.

I would guess there was a syntax error somewhere within that last @foreach, and view compilation failed because of that. Maybe there was another opening directive that you didn't close before calling @endforeach?

Please or to participate in this conversation.