All rules

Sheath rule

blade-forelse-empty-arguments

The @empty branch of a @forelse must be bare; @empty(...) compiles to if(empty(...)) and the loop never closes.
Package
Core
Category
Blade
Default severity
error by default
Auto-fix
Auto-fix available

as a dangerous fix. See Auto-fix.

#Why

@empty is two different directives. Bare, inside a @forelse, it is the empty branch. With arguments, anywhere, it is the empty() conditional that pairs with @endempty. Blade decides by the arguments alone, regardless of position. Giving the forelse branch arguments therefore selects the wrong directive:

@forelse($users as $user) -> foreach ($users as $user):
@empty($users) -> if(empty($users)): <- not the branch!
@endforelse -> endforeach; endif; <- foreach never closed

The foreach is never terminated, the compiled view is invalid PHP, and the error points into storage/ instead of at this line. A space changes nothing: @empty ($users) compiles the same way.

Standalone @empty($x) ... @endempty blocks outside a forelse are the legitimate conditional and are never reported.

#Examples

#Bad

@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty($users)
<li>No users found.</li>
@endforelse
@forelse($posts as $post)
<article>{{ $post->title }}</article>
@empty ($posts)
<p>Nothing published yet.</p>
@endforelse

#Good

@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty
<li>No users found.</li>
@endforelse
{{-- The standalone conditional form, outside any forelse --}}
@empty($records)
<p>Nothing recorded.</p>
@endempty

#Auto-fix

The fix rewrites @empty($users) to a bare @empty. It is dangerous because the arguments are discarded and the intended behavior cannot be inferred. It runs only with --fix --dangerous.

{{-- before --}}
@empty($users)
{{-- after --}}
@empty

#Notes

  • @empty($value) does not satisfy the requirement for a bare @empty branch. See blade-forelse-has-empty for the required @forelse structure.
  • The diagnostic explains both meanings of @empty and identifies the bare form as the correction.

#Related Rules