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.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
