Check if session exists inside blade template - Laravel

Check if session exists inside blade template - Laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Checking Session Existence in Laravel Blade and Controllers

Transitioning from vanilla PHP to Laravel often involves rethinking how you access shared state. In a standard PHP file, checking for a session variable is as straightforward as inspecting the $_SESSION superglobal array:

if (isset($_SESSION['qwick'])) {
    include "checkout-lin.php";
} else {
    include "checkout-nlin.php";
}

This approach relies on raw server variables. In Laravel, however, you interact with the session through a dedicated facade or helper function that abstracts this underlying data structure. To replicate the logic above within a Blade template or controller, you must use the session() helper to inspect the state before deciding which view component to render.

Checking Sessions in Blade Templates

Blade templates are not isolated from application logic; they have access to the full HTTP request object. You can check if a specific session key exists using the old(), session(), or direct session()->has() methods directly within your view file.

For instance, if you have stored an array under the key 'blog' containing user details like email, password, and name, you can conditionally load different include files based on this existence check:

@php
    $isLoggedIn = session()->has('blog');
@endphp

@if ($isLoggedIn)
    @include('includes.loggedinclude')
@else
    @include('includes.notloggedinclude')
@endif

This approach keeps your view logic clean and explicit. You are not relying on implicit variable scoping but rather explicitly verifying the session's state before rendering content.

Alternatively, if you prefer a more concise syntax without the PHP tags, Laravel allows direct access:

@if(session()->has('blog'))
    @include('includes.loggedinclude')
@else
    @include('includes.notloggedinclude')
@endif

This pattern mirrors the isset() check in vanilla PHP but operates within the framework's context. It ensures that if the session key does not exist, you gracefully fall back to the alternative include file without triggering an undefined index notice.

Verifying Sessions in Controllers

The logic for checking session existence is slightly different when moving from a view to a controller. While Blade views often handle rendering decisions, controllers are responsible for business logic and data preparation.

In your controller, you should use the $request object to access the session store. This is consistent with Laravel's request lifecycle management:

if ($request->session()->has('blog')) {
    // Load the logged-in include or route
    return view('checkstatus', [
        'includeFile' => 'includes.loggedinclude'
    ]);
} else {
    // Load the non-logged-in include or route
    return view('checkstatus', [
        'includeFile' => 'includes.notloggedinclude'
    ]);
}

Here, $request->session()->has('blog') returns a boolean true if the key 'blog' exists in the session bag. This is the recommended way to handle this check in controllers because it keeps your logic separate from the view layer. If you attempt to access $request->session()['blog'] directly without checking existence first, Laravel will throw an exception or return null, depending on your configuration and the specific version of the framework being used.

Storing Session Data Correctly

Your source material demonstrates storing an entire array into a single session key:

$vars = [
    "email" => $email,
    "password" => $password,
    "firstname" => $row['firstName'],
    "lastname" => $row['lastName'],
    "id" => $row['id']
];

session()->put('blog', $vars);

This is a valid approach in Laravel. By grouping related data under one key like 'blog', you create a logical namespace for that specific user context. When checking existence later, you are verifying that this entire block of data has been set. If you need to access individual fields like $email within the view, you can do so via:

{{ session('blog')['email'] }}

This maintains encapsulation while allowing dynamic access to nested properties. Remember that session()->put() persists this data across requests as long as the session is active and not cleared.

Trade-offs and Considerations

While checking for session existence is straightforward, there are nuances to consider regarding security and data integrity. Never store sensitive data like passwords in plain text within a session unless you have explicitly hashed them or encrypted them. In modern Laravel applications, it is often better practice to rely on authentication guards (like auth()->check() or Auth::guest()) rather than custom session keys for determining user status.

However, if your application relies on custom data structures like the 'blog' array shown here, the session()->has() method remains the most reliable tool for conditional rendering. It avoids the pitfalls of checking for null values which might be intentionally stored in your session.

Our network