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

jthorpe's avatar

@php tag breaking syntax highlighting when in @foreach loop (Sublime Text 3)

To start, I'm fairly new to Laravel. If anything I say here is incorrect, feel free to correct me.

I have a query that I needed to execute using DB raw, so I wasn't able to use Eloquent. Because of this, I had to do some calculations in my view. I needed to set a variable and then calculate it based on my @foreach loop, but doing so breaks the syntax highlighting after the @php tag in my loop. Has anyone else had this problem?

I'm also open to better ways to do the calculation without it being in the view if anyone wants to suggest it.

This is my first post here, so if the code blocks look messed up I'll fix them right away.

This is my query:

public function byWeek()
        {

            $totals = DB::select(DB::raw("
                    SELECT str_to_date(concat(yearweek(submit_date), 'saturday'), '%X%V %W') as `weekEnding`, 
                    count(claimform1_ID) as Total, 
                    sum(case when claim_approved = 1 then 1 else 0 end) as Compliant,
                    sum(case when claim_approved = 0 then 1 else 0 end) as NonCompliant
                    FROM claim_form1
                    WHERE  submit_date like '20%'
                    Group By weekEnding;
                    ") );

            return view('reports.processed', compact('totals'));

        }

This is part of my view. In Sublime, everything below the @endforeach has wonky syntax highlighting. It's fine if I don't use @php tags, but I think it's the only way to get my results.

                    @php
                        $fullTotal = 0
                    @endphp

                    @foreach($totals as $total)
                        <tr>
                            <td>
                                {{ $total->weekEnding }}
                            </td>
                            <td>
                                {{ $total->Compliant }}
                            </td>
                            <td>
                                {{ number_format((($total->Compliant) / ($total->Total) * 100), 2, '.', '') }}%
                            </td>
                            <td>
                                {{ $total->NonCompliant }}
                            </td>
                            <td>
                                {{ number_format((($total->NonCompliant) / ($total->Total) * 100), 2, '.', '') }}%
                            </td>
                            <td>
                                {{ $total->Total }}
                            </td>
                        </tr>
                        @php
                            $fullTotal += $total->Compliant
                        @endphp
                    @endforeach

Thanks!

0 likes
1 reply
jthorpe's avatar

Well, this is embarrassing. It ended up being the semi-colons I left out when creating the variable.

Sorry for cluttering up the forum!

Please or to participate in this conversation.