Laravel Context vs Session: What's the Difference?
Laravel 11>
Laravel gives us powerful tools to manage data throughout our applications. Two such tools—Context and Session—might seem similar at a glance but serve very different purposes.
This post breaks down the differences between Laravel Context and Session variables, helping you understand when and why to use each one.
What is Laravel Context?
Laravel Context is a feature introduced in Laravel 11 that allows you to share objects or values across classes during a single request without explicitly passing them around.
It works by adding objects into a global "context store", making them available for automatic constructor injection in any class that requires them.
Use Cases
- Dependency injection without bloating method signatures
- Action/service class pipelines
- Temporary state for a single request
- Multi-tenancy
- Cleaner test setups
Example: Using Context
use Illuminate\Support\Facades\Context;
// Add user to the context
Context::add(User::class, $user);
// Resolve a class that needs User
$action = app(UpdateProfile::class);
$action->handle($data);
// Inside UpdateProfile class
class UpdateProfile
{
public function __construct(protected User $user) {}
public function handle(array $data)
{
$this->user->update($data);
}
}
🔒 Context only exists during the current request or console command. It’s gone once the request is done.
What is Session?
Session is Laravel's way of storing data that persists across multiple requests, usually for user-facing state like:
- Flash messages
- Authentication
- Preferences (language, theme)
- Multi-step form progress
- Shopping carts
Session data is stored in drivers like file
, database
, or Redis
and is tied to the user’s session cookie.
Use Cases
- Tracking logged-in users
- Showing messages after a redirect
- Persisting form state between steps
- Storing user preferences
- Cart and checkout flows
Example: Using Session
// Store a success message
session()->flash('success', 'Profile updated!');
// Save a language preference
session(['locale' => 'pt']);
<!-- Show flash message in a Blade view -->
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
✅ Session persists across requests and is ideal for user-facing data that needs to "stick around."
Laravel Context vs Session: Quick Comparison
Feature | Laravel Context | Session |
---|---|---|
Scope | Single request | Across multiple requests |
Lifetime | In-memory (ephemeral) | Persistent (until session expires) |
Storage | In request container | File, database, Redis, etc. |
Use Case | Service layers, pipelines, testing | Flash messages, auth, user prefs |
Developer or User? | Developer-side (internal logic) | User-side (UI logic) |
Inject via Constructor | ✅ Yes | ❌ No |
When to Use Which
Goal | Use This |
---|---|
Share a model between service/action classes | Context |
Store user preferences or form progress | Session |
Inject data into a class without passing manually | Context |
Show success/error messages after a redirect | Session |
Share a tenant or user in a multi-tenant app | Context |
Remember app state between requests/pages | Session |
🧪 Testing Context vs Session
In your tests, Context is extremely useful for injecting dependencies cleanly:
Context::add(User::class, User::factory()->create());
$this->app->make(UpdateProfile::class)->handle([
'name' => 'Jane Doe',
]);
Whereas Session is handy when testing things like flash messages or redirects:
$response = $this->post('/profile/update', [
'name' => 'Jane Doe',
]);
$response->assertSessionHas('success', 'Profile updated!');
Caution: Don’t Overuse Context
While Context is powerful, it can hide dependencies. Be intentional. If you overuse it, your classes may become harder to test and understand.
Tip: Use Context for request-scoped state only. Avoid putting general app config or user preferences in there—Session is better for that.
Final Thoughts
Laravel Context and Session are both incredible tools—but they shine in different places.
- Use Context when you want to simplify internal dependency injection within a single request.
- Use Session when you need to store data between requests for the user experience.
By understanding the difference, you'll write cleaner, more maintainable apps that follow Laravel’s design philosophy.
Let me know if you'd like examples tailored to Livewire, Inertia, or other parts of the TALL stack!