Level 58
Certainly! The issue here is about the case sensitivity and usage of the session helper or facade in Laravel.
Explanation
- Class "session" not found error happens because
sessionwith a lowercasesis not a class but a helper function in Laravel. - When you use
session::get()(with double colon), Laravel looks for a class namedsession, but the correct class isSession(capital S) or you should use the global helper function.
Solution 1: Use the session() helper
The most common and recommended method in Blade:
@if ($message = session('flash'))
<div class="alert alert-success">
{{ $message }}
</div>
@endif
- Here,
session('flash')uses the global helper function to retrieve the value from the session.
Solution 2: Use the Session facade (add the correct import)
If you want to use the facade approach, make sure to use Session with a capital S and include the import at the top of your blade file:
@php
use Illuminate\Support\Facades\Session;
@endphp
@if ($message = Session::get('flash'))
<div class="alert alert-success">
{{ $message }}
</div>
@endif
Summary
- Recommended: Use the
session()helper. - Only use
Session::get()if you import theSessionfacade and use correct casing.
So just change:
@if($message = session::get('flash'))
to:
@if($message = session('flash'))
or, if you want the facade:
@php
use Illuminate\Support\Facades\Session;
@endphp
@if($message = Session::get('flash'))
Let me know if you have any further questions!
1 like