DEV Community

Shariful Ehasan
Shariful Ehasan

Posted on

@auth vs @if(auth()->check()) - which one are you still writing and why?

We've all done it. Some of us still do.

But if you're writing @if(auth()->check()) just to show/hide something for logged-in users, you're making your Blade files noisier than it needs to be.

Let's fix that — permanently.

The Old Way: @if(auth()->check())

@if(auth()->check())
    <p>Welcome back, {{ auth()->user()->name }}!</p>
    <a href="/dashboard">Dashboard</a>
@else
    <a href="/login">Log in</a>
    <a href="/register">Register</a>
@endif
Enter fullscreen mode Exit fullscreen mode

It works. It's valid PHP.
But it's generic, verbose, and hides intent.

The Right Way: @auth and @guest

@auth
    <p>Welcome back, {{ auth()->user()->name }}!</p>
    <a href="/dashboard">Dashboard</a>
@endauth

@guest
    <a href="/login">Log in</a>
    <a href="/register">Register</a>
@endguest
Enter fullscreen mode Exit fullscreen mode

Or even cleaner with @else:

@guest
    <a href="/login">Log in</a>
    <a href="/register">Register</a>
@else
    <p>Welcome back, {{ auth()->user()->name }}!</p>
    <a href="/dashboard">Dashboard</a>
@endguest
Enter fullscreen mode Exit fullscreen mode
Feature @auth / @guest @if(auth()->check()) Winner
Readability Instant understanding Requires reading the condition @auth
Brevity Less code, less noise More typing @auth
Intent Directive name says exactly what it does Generic if @auth
Multiple guards @auth('admin') — clean auth()->guard('admin')->check() — ugly @auth
Safety Can't accidentally call method on null user Easy to forget check() first @auth
Laravel recommendation First-class citizen Only for complex cases @auth

Real Navigation Example (What Every App Has)

<nav class="flex items-center space-x-6">
  @auth
    <x-dropdown>
      <x-slot name="trigger">
        <button class="flex items-center space-x-3">
          <img src="{{ auth()->user()->avatar }}" class="w-8 h-8 rounded-full">
          <span>{{ auth()->user()->name }}</span>
        </button>
      </x-slot>

      <x-dropdown-link href="/profile">Profile</x-dropdown-link>
      <x-dropdown-link href="/settings">Settings</x-dropdown-link>
      <x-dropdown-link href="/logout" method="POST">Log Out</x-dropdown-link>
    </x-dropdown>
  @else
    <a href="/login" class="btn">Log in</a>
    <a href="/register" class="btn btn-primary">Sign up</a>
  @endauth
</nav>
Enter fullscreen mode Exit fullscreen mode

Clean. Professional. Self-documenting.

When You Actually Need @if(auth()->check())

Only one valid reason exists:
You need to combine authentication with other logic.

@if(auth()->check() && auth()->user()->is_admin)
    <a href="/admin" class="text-red-600 font-bold">Admin Panel</a>
@endif

@if(auth()->check() && auth()->user()->hasActiveSubscription())
    <span class="badge badge-pro">PRO</span>
@endif
Enter fullscreen mode Exit fullscreen mode

@auth can't do complex conditions — and that's fine. It's not supposed to.

The Hybrid Pattern Everyone Actually Uses

@auth
    <p>Hello, {{ auth()->user()->name }}!</p>

    @if(auth()->user()->is_admin)
        <a href="/admin">Admin Dashboard</a>
    @endif

    @if(auth()->user()->hasRole('moderator'))
        <span class="text-purple-600">Moderator</span>
    @endif
@endauth

@guest
    <p>Please <a href="/login">log in</a> to continue.</p>
@endguest
Enter fullscreen mode Exit fullscreen mode

Final Recommendation

Use @auth and @guest for every simple authentication check.
Use @auth('admin') when you need a specific guard.

Fall back to @if(auth()->check() && ...) only when you have to combine the login state with other conditions (roles, subscriptions, etc.).
And inside class bindings, just write auth()->check().

Stop writing @if(auth()->check()) for basic checks.
Laravel built a cleaner, safer, more expressive tool for exactly this purpose. Use it.

Top comments (0)