Blade Directives (@if, @foreach, @include)
Blade directives are shorthand for common PHP control structures, written with an @ prefix instead of PHP's usual <?php ?> tags. They make views far easier to scan, since logic reads almost like plain English.
Conditional Directives
Blade offers direct equivalents for PHP's conditional statements, letting a view decide what to display without breaking out of the templating syntax. This keeps HTML and logic visually distinct while still being fully dynamic.
Looping Directives
Displaying a list of records, like blog posts or products, is one of the most common tasks in any view. Blade's looping directives handle this cleanly, and a special $loop variable is available inside them for tracking index, position, and iteration count.
| Directive | What It Does |
|---|---|
@if / @elseif / @else / @endif | Renders content conditionally |
@foreach ... @endforeach | Loops over a collection or array |
@forelse ... @empty ... @endforelse | Loops over data, with a fallback if it's empty |
@include('partial') | Inserts another Blade view inline |
@auth / @guest | Checks whether a user is logged in |
Reusing Views with @include
- Breaks large views into smaller, reusable partials, like a card or alert box
- Keeps repeated markup, such as navigation, in a single source file
- Accepts an optional array of data passed only to the included view
Directives handle logic inside a single view, but most real applications also need a consistent page structure across many views — which is exactly what Blade layouts and sections solve.
Conditionals in Real Templates
Here is how the conditional directives look inside an actual view. The data comes from the controller as $user and $cart.
@if ($cart->isEmpty())
<p>Your cart is empty.</p>
@elseif ($cart->count() > 10)
<p>Big order! Free delivery applied.</p>
@else
<p>{{ $cart->count() }} items in your cart.</p>
@endif
@unless ($user->hasVerifiedEmail())
<p>Please verify your email address.</p>
@endunless
@isset($coupon)
<p>Coupon applied: {{ $coupon->code }}</p>
@endisset
@unless is simply the opposite of @if, and @isset checks that a variable exists and is not null, which avoids "undefined variable" errors when data is optional.
When there are many fixed options, @switch with @case, @break and @default reads more cleanly than a long chain of @elseif lines.
Loops and the $loop Variable
<ul>
@forelse ($orders as $order)
<li>
{{ $loop->iteration }}. Order #{{ $order->id }}
@if ($loop->first) (latest) @endif
@if ($loop->last) (oldest shown) @endif
</li>
@empty
<li>No orders yet.</li>
@endforelse
</ul>
Useful properties include $loop->index (starts at 0), $loop->iteration (starts at 1), $loop->count, $loop->remaining, and $loop->parent for nested loops. Inside any loop, @continue and @break work like their PHP equivalents, and both accept a condition, for example @continue($order->cancelled).
Variations of @include
| Directive | Behaviour |
|---|---|
@include('partials.card', ['item' => $item]) | Includes a view and passes extra data |
@includeIf('partials.promo') | Includes only if the view file exists |
@includeWhen($user->isAdmin(), 'partials.admin-bar') | Includes only when the condition is true |
@includeUnless($user->isAdmin(), 'partials.upgrade') | Includes only when the condition is false |
@each('partials.row', $orders, 'order', 'partials.none') | Renders a partial for each item, with a fallback view |
Included views automatically receive every variable available in the parent view, so passing an array is only needed for extra or renamed data. For reusable pieces with their own props and slots, Blade components are often a better fit than plain includes.
Common Blade Mistakes and Safe Habits
- Using
{!! !!}for user input: it prints raw HTML and opens the door to cross-site scripting. Use{{ }}, which escapes output, unless the content is trusted and already sanitised. - Missing
@csrfin forms: POST requests then fail with a 419 error. - Heavy logic in views: database queries or complex calculations belong in controllers, view models or components.
- Forgetting the closing directive: a missing
@endifor@endforeachcauses a syntax error that can point to the wrong line. - Stale compiled views: after big changes,
php artisan view:clearremoves the cached compiled templates.
Blade is not interpreted on every request. The first time a view is rendered, Laravel compiles each directive into plain PHP, for example turning @if ($x) into a normal PHP if block, and saves the result in storage/framework/views. Later requests reuse that compiled file until the template changes. This is why directives add almost no overhead, and why a broken directive shows an error that points to compiled PHP.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.