Weird Laravel flash session issue
I am facing a very strange problem and cannot find anything. I am trying very simply by using flash message in Laravel to display a message to the user if a condition was not met.
Basically this is the controller code I am using:
Session::flash('error', 'error message');
return redirect()->back();
// this didn't work either
// return redirect()->action('Controller@method', $var)->with('error', ['error message']);
The thing is, it works when I var_dump the session in the view, but not without it. This is the view:
{{ Session::get('error') }} // does not work
{{ dd(Session::get('error')) }} // works!
This is a very simple thing, but I don't know why it is going wrong.
Any helpers? :) Thanks!
+3
source to share
1 answer
You should debug this way
Clear all files in the folder sessions
In your controller, you can create a flash message using either of two methods.
Session::flash('error', 'error message');
return redirect()->back();
or
return redirect()->action('Controller@method', $var)->with('error', ['error message']);
Then in your blade
@if (Session::has('error'))
<div class="alert alert-info">
{{ Session::get('error') }}
</div>
@endif
+3
source to share