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

MislavNovalic's avatar

Error on the 29 day of series "30 days of Laravel"

Hi guys, I did not know where is the error I have checked all @foreach loops and I cannot find it is there another way to do this ? Error is in view somewhere. I cannot run that final project :(

Illuminate\Contracts\View\ViewCompilationException

Malformed @foreach statement.

0 likes
7 replies
LaryAI's avatar
Level 58

The error message "Malformed @foreach statement" typically indicates a syntax issue in your Blade template. Here are some steps to troubleshoot and resolve this issue:

  1. Check the Syntax: Ensure that your @foreach loop is correctly structured. The basic syntax should look like this:

    @foreach($items as $item)
        <!-- Your code here -->
    @endforeach
    
  2. Ensure Proper Closure: Make sure every @foreach has a corresponding @endforeach. A missing @endforeach can cause this error.

  3. Check for Nested Loops: If you have nested @foreach loops, ensure each one is properly closed with @endforeach.

  4. Look for Typos: Double-check for any typos or missing characters in your @foreach statement.

  5. Blade Comments: If you have comments within your Blade file, ensure they are properly formatted. Blade comments should be written as:

    {{-- This is a Blade comment --}}
    
  6. Debugging: If the error persists, try isolating the problem by commenting out sections of your Blade file to identify where the issue is occurring.

  7. Example Correction: Here’s an example of a correctly formatted @foreach loop:

    @foreach($users as $user)
        <p>{{ $user->name }}</p>
    @endforeach
    

By following these steps, you should be able to identify and fix the malformed @foreach statement in your Blade template. If you continue to experience issues, consider sharing the specific section of your Blade file where the error occurs for more targeted assistance.

1 like
MislavNovalic's avatar

I have a new error now it is this :

Undefined variable $tag

But tag is added in @props so I do not get it

Snapey's avatar

we can only imagine....

1 like
MislavNovalic's avatar

@Snapey maybe you know what is going on I am watching this video second time . Is there any way to test this ? Thanks !

Snapey's avatar

@MislavNovalic I dont know whats going on. Try sharing stuff, like your code or the error message (full)

1 like
MislavNovalic's avatar

Thanks, but I will probably continue without tags for this project, I compared file from Github and mine it is the same. Error is in some other place...I think I need to find it by myself.

Please or to participate in this conversation.