Ad

Laravel 5 Redirect With Message

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?

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad