GabrielFiel
GabrielFiel

Reputation: 67

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?

Upvotes: 2

Views: 3178

Answers (3)

GSM
GSM

Reputation: 51

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');

Upvotes: 0

Shakti Phartiyal
Shakti Phartiyal

Reputation: 6254

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

Upvotes: 6

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think you need to update your code like :

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

@endif

Upvotes: 0

Related Questions