@unless

Renders a block only when an expression is falsy, reading as the inverse of a conditional.

Signature
@unless ($condition) ... @endunless

Parameters

ParameterTypeDescription
$conditionmixedAny PHP expression. The block renders when the expression is falsy; the directive compiles to a negated if.

Return value

Nothing — the block is emitted into the rendered output when the expression evaluates to false.

Errors

Closing the block with @endif instead of @endunless leaves the compiled PHP unbalanced and raises a ParseError in the cached view.

@unless is a convenience wrapper around a negated condition: the block renders when the expression is falsy. It exists so templates can read as English — @unless (Auth::check()) instead of @if (! Auth::check()) — and it compiles to an ordinary negated if.

When to reach for it

Use it for a single guard clause with no alternative branch: a sign-in prompt for visitors, a warning shown while a record is unverified, a call to action that disappears once a subscription exists. The moment you need a second branch, switch back to @if — there is no @elseunless, and stacking an @else under an inverted condition is the fastest way to make a template unreadable.

What trips people up

The closing directive is @endunless, not @endif. Mismatching them compiles to unbalanced PHP and fails with a parse error in the cached file rather than in your template.

Also watch for double negatives: @unless (! $user->isBanned()) is technically valid and practically unreadable. If the condition already contains a negation, invert the wording in a model method instead and go back to a plain @if.

Example

Example · php
<?php

use Illuminate\Support\Facades\Blade;

$template = <<<'BLADE'
@unless ($subscribed)
    <p>Subscribe to receive release notes.</p>
@endunless
BLADE;

echo Blade::render($template, ['subscribed' => false]);
// <p>Subscribe to receive release notes.</p>

echo Blade::render($template, ['subscribed' => true]);
// (nothing)

When to use it

  • Show a sign-in prompt to visitors who are not authenticated
  • Display an 'unverified email' banner until the address is confirmed
  • Hide an upgrade call to action once the team already has a paid plan

Frequently asked questions

What is the difference between @unless and @if in Blade?

@unless renders its block when the expression is falsy, so it is shorthand for @if (! $condition). Both compile to the same PHP if statement; @unless simply keeps the negation out of the template.

Can I use @else inside a Blade @unless block?

No — treat @unless as a single-branch directive. Laravel documents it only with @endunless, and a template that needs two branches reads far better written as @if with the positive condition first.

Why doesn't @unless fire for my empty collection?

Because a Collection object is truthy even when it holds no items, so the negated condition is false. Test the collection explicitly with $items->isEmpty(), or use @forelse, which has a built-in empty branch.

More examples

The documented authentication guard

This is the form the Laravel documentation uses. It needs a booted application because Auth::check() resolves the authentication guard from the container. In a real template @guest ... @endguest expresses the same intent with less machinery, and accepts a guard name as an argument.

Example · php
<?php

// resources/views/partials/nav.blade.php
$nav = <<<'BLADE'
@unless (Auth::check())
    You are not signed in.
@endunless
BLADE;

echo view('partials.nav')->render();

Empty collections are truthy

Branch A never renders: an empty Collection is still an object, and every object is truthy in PHP. Branch B asks the collection directly and behaves as expected. The same trap catches empty Eloquent collections returned by a relation, which is the usual reason an 'is empty' block silently disappears.

Example · php
<?php

use Illuminate\Support\Facades\Blade;

$template = <<<'BLADE'
@unless ($items)
    A: no items
@endunless

@unless ($items->isNotEmpty())
    B: no items
@endunless
BLADE;

echo Blade::render($template, ['items' => collect()]);
// B: no items

When to go back to @if

@unless is documented as a single-branch convenience. As soon as an alternative branch appears, the reader has to invert the condition mentally to follow the else, so the @if form is the one to keep. Reserve @unless for guard clauses that render nothing when the condition holds.

Example · php
<?php

use Illuminate\Support\Facades\Blade;

// Hard to read: negation plus an else branch.
$bad = <<<'BLADE'
@unless ($order->isPaid())
    Awaiting payment
@else
    Paid
@endunless
BLADE;

// Clear: the positive case comes first.
$good = <<<'BLADE'
@if ($order->isPaid())
    Paid
@else
    Awaiting payment
@endif
BLADE;

echo $good;

Discussion

  • Be the first to comment on this lesson.