Reputation: 5053
I have a Laravel Application, and something strange is happening related to the session variables. I have a method in a controller that checks some inputs and depending of some conditions it decides if continues or just return back (and right here where is my problem), when I make return redirect()->back()
I want so send a variable that is a Json, so briefly I have this:
$msg=json_encode([
'status' => 'failed',
'field'=>'mail'
]);
return redirect()->back()->with('msg',$msg);
So, it redirects very well, but then in the view I have:
@if ( Session::has('msg') )
//come stuffs
@endif
But it seems likes the msg
variable is not passed. According to the Laravel documentation when one uses redirect back
the variables are accesible trough Session
Upvotes: 1
Views: 162
Reputation: 1543
I think you need to use $request->session()->put('key', 'value');
before you initiate the redirect
read the documentation at HTTP Session
Upvotes: 0
Reputation: 606
Its' working for me when I tested. Maybe your session is not storing data. Please check whether your session is working or not. My tested output
@if ( Session::has('msg') )
{{ session('msg') }}
@endif
Upvotes: 1
Reputation: 2356
I also encountered this problem on a project a while back with Laravel 5.2. I don't know if it is a bug or whether it has been fixed but I ended up getting around it by not using redirect()->back()
. I think it might be related to the browser's caching mechanism. I would be interested if you end up finding the cause.
Upvotes: 0