#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

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.

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

DirectiveBehaviour
@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 @csrf in 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 @endif or @endforeach causes a syntax error that can point to the wrong line.
  • Stale compiled views: after big changes, php artisan view:clear removes 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.

Explore Course