Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 flash message flashes again after navigating away and using back button

Tags:

laravel

In my controller im trying to redirect back with a Flash message in laravel 5. This all works great. The problem is no matter how i set it up the flash message always re-appears if i navigate away and then come back by using the browsers 'back button'.

So i have a users list with a delete button next to each user. When i click the delete button it calls my controller method to destroy. I made a little conditional in there that redirects back with an error if i am trying to delete an owner. I tried the following different ways to redirect back but with all i wind up with the same problem that the message shows again after navigating away and coming back via the browsers 'back button'.

1

return Redirect::route('users')->with('error_message', 'Some error msg');

2

Session::flash('error_message', 'Some error msg');
return Redirect::to('users'); 

In my view i pick it up like this:

@if (Session::has('error_message'))
     {{ Session::get('error_message') }}
@endif

So that works good, i get the message. But as soon as for example i click a user in my user list to go to the details page and press the browser 'back button', the message flashes again. I dont get why it keeps flashing that data, i was under the impression that it just flashes one time.

Even if i try to clear it right after displaying (like below), it doesnt matter, it will always re-appear?

{!! Session::forget('error_message') !!}
like image 359
Ahzrael Avatar asked May 04 '15 20:05

Ahzrael


1 Answers

Generally, when the user clicks the back button in the browser, the browser tries to display the contents of the previous page without reloading it. So it's likely not Laravel flashing to the session again, but the browser trying to help you out by caching the page for you.

like image 108
Joel Hinz Avatar answered Oct 03 '22 07:10

Joel Hinz