Reputation: 4539
I have a controller with this redirect at the end of a method:
// Setup Response message
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
return redirect()->back()->with('message', $msg);
The response looks like this:
array:5 [▼
"_flash" => array:2 [▶]
"_token" => "stMmai4OuWI2QlhYGNzxSDyb1qINLfD3RWsoM8mx"
"_previous" => array:1 [▶]
"url" => []
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 2
]
I am stumped as to what is causing the message to not be flashed through to session.
If I do this:
session()->flash('message', $msg);
dd(session()->all());
// Setup Response message
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
return redirect()->back()->with('message', $msg);
prior to the redirect, the session contains the message.
I even moved the two routes (POST and GET) outside of all Auth groups / Middleware, same result.
What could be clearing the session message?
Upvotes: 1
Views: 5105
Reputation: 3390
I had the same issues, and my code looked like below, I previously had a database operation in the try cache block, I removed and the data passed was no longer working, After numerous trials, I realized I have DB transactions, so I removed them and the session/flash data redirect()->back()->with($inputArray);
started working
DB::beginTransaction();
try {
if ($coupon == null) {
$this->results['failure'] = __('messages.failure_coupon_is_invalid');
return redirect()->back()->with($this->results);
}
setHasDiscount($coupon);
$this->results['success'] = __('messages.success_coupon_applied');
DB::commit();
return redirect()->back()->with($this->results);
} catch (Exception $exception) {
DB::rollBack();
$this->results['failure'] = $exception->getMessage();
return redirect()->back()->with($this->results);
}
Upvotes: 0
Reputation: 4539
@Udo was right, it seems a change in Laravel 5.8. This worked:
redirect()->back()->with('message', $msg);
Upvotes: 1
Reputation: 2823
A better way of flashing data to the current session is using the global session()
helper. Using this method your code should look like this:
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
session()->put('message', $msg);
return redirect()->back();
NOTE:
Redirecting back using with(['message' => $msg])
passes the variable $message
to a view or route. So you can access it using that variable name in the view you redirected to.
On the other hand, using either session()->put('message', $msg);
or session()->flash('message', $msg);
both flashes the data with key message
into the current user session. So you can access the session data using session()->get('message')
UPDATE:
From my research, As of Laravel version 5.8, I think withInput()
is now reserved for Request data (that is data pass to your controller via GET
or POST
request) and with()
is reserved for passing data to a route or view either through route($route)->with()
or view($view)->with()
.
So the issue with your code may be changes in the new update - version 5.8
Upvotes: 0
Reputation: 144
Try it with return redirect()->back()-withErrors('message', $msg)
EDIT: You use the ErrorBag here - maybe this works as it seems something is killing your session flash.
regards Jens
Upvotes: 0