Laravel 5: Change navbar if user is logged

Laravel 5: Change navbar if user is logged

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5: Change navbar if user is logged in

In a typical MVC architecture, separating concerns between routing, logic, and presentation is key to maintainability. When handling authentication states like "logged in" versus "guest," relying on isset($user) inside your Blade templates is a common starting point for beginners, but it often indicates a fundamental misunderstanding of how Laravel's session data works. While passing a $user object to the view and checking its existence is technically functional, it forces every partial or layout file to manually re-evaluate authentication logic that Laravel has already resolved.

The more idiomatic approach in Laravel 5 involves leveraging the Auth facade or helper functions directly within your views. This shifts the burden of state management away from your templates and onto the framework's core utilities. Instead of guessing whether a user object was passed down, you should explicitly check for authentication status using Auth::check() or Auth::user().

Consider the following layout structure in app.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel App</title>
</head>
<body>
    @if (Auth::check())
        <!-- User is authenticated -->
        <nav class="navbar">
            <ul>
                <li><a href="/notifications">Notifications</a></li>
                <li><a href="/profile">Profile</a></li>
                <li><a href="/logout" method="POST">Logout</a></li>
            </ul>
        </nav>
    @else
        <!-- User is a guest -->
        <nav class="navbar-guest">
            <ul>
                <li><a href="/login">Login</a></li>
                <li><a href="/register">Register</a></li>
            </ul>
        </nav>
    @endif

    <!-- Your main content goes here -->
</body>
</html>

This pattern keeps your app.blade.php file as a single source of truth for the navbar's conditional logic. You can then create reusable partials, such as partials.navbar-logged.blade.php, to encapsulate the specific HTML structure for authenticated users. This separation ensures that if you need to change the logout button's styling or add a user avatar later, you only touch one file rather than hunting for logic scattered across multiple includes.

A critical trade-off to consider here is the coupling between your views and your controllers. When you pass $user explicitly from every controller action, you are tightly coupling your presentation layer to specific data transfer objects. If you ever switch from a standard User model to a custom authenticated model or implement a multi-tenant system where the user object structure changes, your templates might break if they rely on specific properties being present in that passed variable. By using Auth::user(), you access the authenticated model instance directly from the session, decoupling the view from the controller's data passing strategy.

Another potential pitfall is handling the logout action itself. In Laravel 5, a simple <a> tag pointing to the logout route is insufficient because the form method must be POST to prevent Cross-Site Request Forgery (CSRF) attacks. If you include a logout link in your navbar, ensure it either submits a hidden form or uses the form.post() helper if you are using the Blade form helpers. Neglecting this detail can lead to security vulnerabilities regardless of how cleanly you structure your navigation logic.

Finally, while checking for $user availability works, it is often a symptom of trying to optimize prematurely. In the early stages of a Laravel project, readability and adherence to framework conventions usually outweigh micro-optimizations regarding data passing. The Auth facade provides a clear intent: "Am I currently authenticated?" This clarity makes codebases easier for other developers to read and maintain, which is often more valuable than saving a few bytes of memory or avoiding an extra variable assignment in the controller. For further insights on building scalable layouts and managing authentication flows effectively, you might explore resources like https://laravelcompany.com.

Our network