'Laravel - Browser displays message again when I press back button

Every time I create a new category in my application, it displays a message of success. Here is my code:

return redirect('dashboard/categorias')->with('message', 'Categoria criada com sucesso!');

I know it's not a big deal, but when I create a new category then I go to another page and press back button on browser, it shows the message again.

Here is my view:

@if(session('message'))
        <script>Materialize.toast('{{session('message')}}', 4000)</script>
        {{ Session::flush() }}

@endif

What should be done to not show this message again?



Solution 1:[1]

You should go for session flashing in laravel, once the session datat is read it is not available again:

$request->session()->flash('message', 'Categoria criada com sucesso!');

Or

Session::flash('message', 'Categoria criada com sucesso!');

UPDATE

Your issue seems to be of caching of the page, use the following headers :

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

before sending out your response.

You can even create a middleware to not to use the cache, check the answer here:

https://stackoverflow.com/a/42057397/2952213

Solution 2:[2]

I think you need to update your code like :

@if(Session::has('message'))
        <script>Materialize.toast({{ Session::get('message') }}, 4000)</script>
        {{ Session::flush() }}

@endif

Solution 3:[3]

add a few lines in your view

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

and after you display your message

@session()->forget('message');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 AddWeb Solution Pvt Ltd
Solution 3 GSM