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.
#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:
@defaultas the first branch. Only the first@casere-enters the open statement;@defaultalways compiles as a separate<?php default: ?>block, so a switch that opens onto@defaultis broken even though it looks reasonable.- A
@switchwith no@caseat 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
@switchor into each branch; there is no valid place for it between@switchand the first@case. - A
@switchalso requires a matching@endswitch. See blade-unclosed-directives.
#Related Rules
- blade-unclosed-directives - unpaired block directives
- blade-valid-directive-arguments - core directives compiled with missing or malformed arguments