All rules

Sheath rule

blade-switch-structure

@switch must open directly onto its first @case; content between them, or @case/@default outside a switch, compiles to invalid PHP or literal page text.
Package
Core
Category
Blade
Default severity
error by default
Auto-fix
Manual fix

#Why

Blade compiles @switch($x) to <?php switch($x): and then stays inside PHP until the first @case closes the tag. Whatever sits between the two is compiled into the open switch statement, where PHP allows nothing but whitespace:

@switch($type) -> <?php switch($type):
<div>leading</div> -> <div>leading</div> <- inside the open PHP!
@case(1) -> case (1): ?>

The compiled view is invalid PHP, and the parse error points into storage/ instead of at this template. The same mechanic breaks two more shapes:

  • @default as the first branch. Only the first @case re-enters the open statement; @default always compiles as a separate <?php default: ?> block, so a switch that opens onto @default is broken even though it looks reasonable.
  • A @switch with no @case at all, which leaves the opening statement dangling.

Outside a @switch, the branch markers misfire in the other direction: @case(1) never compiles and the page shows the literal text case (1): ?>, while a stray @default compiles to invalid PHP that fails at first render.

Blade comments and whitespace between @switch and @case are valid because comments are stripped before statements compile.

#Examples

#Bad

@switch($type)
<div>Pick one:</div>
@case(1)
One
@break
@endswitch
@switch($type)
@default
Fallback first does not work
@endswitch
@case(1)
A case with no @switch renders as literal text.
<p>Done.</p>
@default

#Good

@switch($type)
@case(1)
One
@break
@case(2)
Two
@break
@default
Other
@endswitch
@switch($type)
{{-- comments are stripped before statements compile --}}
@case(1)
One
@break
@endswitch
<p>Escape the marker when you mean the text: @@case(1)</p>

#Notes

  • Move any shared markup above the @switch or into each branch; there is no valid place for it between @switch and the first @case.
  • A @switch also requires a matching @endswitch. See blade-unclosed-directives.

#Related Rules