Sheath rule
blade-forelse-has-empty
Reports a
@forelse block with no @empty branch.
#Why
@empty is not optional. @forelse compiles the loop and the empty branch as
one unit, so leaving @empty out produces PHP that does not parse, and the page
fails with a syntax error in the compiled view.
If the empty case genuinely needs no output, @foreach is the directive for
that.
#Examples
#Bad
<!-- @forelse without @empty compiles to invalid PHP -->
@forelse($users as $user)
<p>{{ $user->name }}</p>
@endforelse
<!-- Missing @empty block -->
@forelse($items as $item)
<li>{{ $item->title }}</li>
@endforelse
#Good
<!-- @forelse with @empty provides good UX -->
@forelse($users as $user)
<p>{{ $user->name }}</p>
@empty
<p>No users found.</p>
@endforelse
<!-- Or use @foreach if you don't need empty handling -->
@foreach($users as $user)
<p>{{ $user->name }}</p>
@endforeach
#Notes
- If you don't need empty state handling, use
@foreachinstead - The
@emptyblock can contain any content: messages, empty states, CTAs, etc. - A missing branch may indicate that
@foreachwas intended
#Related Rules
- blade-prefer-forelse - Prefer @forelse over @if+@foreach for empty checks