Check if session exists inside blade template - Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Checking for Session Existence in Blade Templates of Your Laravel Application
Introduction: In today's world where security is paramount, the usage of sessions has become a crucial aspect in web development. As a Laravel developer, you might have encountered scenarios where you need to check if a session exists or not. This blog post aims to provide comprehensive guidance on how to accomplish that within your Laravel application, specifically when working with Blade templates.
Checking for Session Existence in Blade Templates: In order to check if a specific session (or all sessions) exist within your Laravel application's Blade template, you can leverage the built-in Laravel features and some simple syntax. The following code snippet shows how to handle the conditions based on whether a session exists or not:
@if(session()->has('blog'))
{{-- Display blog content --}}
@foreach($blog as $item)
{{-- Display each item in the blog array --}}
@endforeach
@else
{{-- No blog session found, display alternative content --}}
No blog available.
@endif
In this example, we are checking if a session called 'blog' exists. The "has" method checks for the specified session and either displays the contents of the array or an alternative message if it doesn't exist. You can modify this code to check for different sessions by changing the session name in the `session()->has('blog')` line.
Checking for Named Session Existence in Controllers: Now, let's explore how to handle named sessions in your Laravel application's Controllers. A Controller is a class that processes an HTTP request and returns a response back to the user. Here's an example of checking if a session called 'specific_session' exists before performing any necessary actions:
public function index() {
// Check session existence
if (session()->has('specific_session')) {
// Perform required actions for users with the specific_session
return redirect('/specific-page');
} else {
// Redirect or perform alternative actions for others
return redirect('/alternative-page');
}
}
In this example, we are checking if a session called 'specific_session' exists before sending the user to either the 'specific-page' route or the 'alternative-page' route. This can be useful for implementing user permissions or personalized pages based on their sessions in your Laravel application.
Conclusion: Checking for session existence is an essential skill while working with Laravel applications, especially when dealing with sensitive data and user permissions. By leveraging the built-in Laravel features and techniques, you can easily accomplish this task in both Blade templates and Controllers to provide a secure and personalized experience for your users.