← Back to Course

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.

DirectiveWhat It Does
@if / @elseif / @else / @endifRenders content conditionally
@foreach ... @endforeachLoops over a collection or array
@forelse ... @empty ... @endforelseLoops over data, with a fallback if it's empty
@include('partial')Inserts another Blade view inline
@auth / @guestChecks whether a user is logged in

Reusing Views with @include

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.

Explore Course