Can someone explain why the code below does not work when it redirects to another page?
return redirect()->route('homepage')->with('message', 'I am so frustrated.');
The redirect works as expected, but the message does not appear.
The view looks like:
@if ( session()->has('message') )
<div class="alert alert-success alert-dismissable">{{ session()->get('message') }}</div>
@endif
When I change it to:
return redirect()->route('contact')->with('message', 'I am so frustrated.');
, which is the same as redirect()->back(), everything works fine and the message is displayed. What is the difference between redirect back()
and to()
another view?
If you're on Laravel 5.2, make sure all the routes you're using that access session data are contained in the web
middleware group.
If your contact
route is inside the web
middleware group, but your homepage
route is not, that would explain your issue.
I am currently on Laravel 5.7 HTTP Redirects and this code worked for me in the Controller:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
return $this->registered($request, $user) ?:
redirect($this->redirectPath())->with('success',
'An activation link was send to your email address.');
}
And in the View (etc.blade.php) i do:
@if(session('success'))
<span class="alert alert-success" role="alert">
<strong>{{ session('success') }}</strong>
</span>
@endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With