Home > database >  What is the "Malformed @foreach statement." error on Laravel's Blade?
What is the "Malformed @foreach statement." error on Laravel's Blade?

Time:12-02

I have this very basic setup...

Users.php (Laravel Livewire component)

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\User;

class Users extends Component
{
    public $users;

    public function render()
    {
        $this->users = User::orderBy('name', 'asc')->get();
        return view('livewire.users');
    }
}

users.blade.php

@foreach ($users as $user)
    <div>{{ $user }}</div>
    <hr />
@foreach

However, it is throwing the exception Illuminate \ Contracts\View\ViewCompilationException with the following error:

Malformed @foreach statement.

Did I miss something? What does this error mean?

CodePudding user response:

You need to close your @foreach statement with @endforeach before starting new foreach. Same applies for @if @endif or any other Blade directives.

@foreach ($users as $user)
    <div>{{ $user }}</div>
    <hr />
@endforeach

Reference: https://laravel.com/docs/9.x/blade#loops

  • Related