Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danWilcz's avatar

My periodic Ajax call deletes session flash data.

My periodic Ajax call deletes session flash data.

I have javascript that makes Ajax call (it calls call my api) every 3 seconds to update user page.

I am returning a view with flash data that is supposed to be kept for the next requests. It seems the Ajax call clears it and the next request can not be completed.

Is the a way to prevent this?

One way I can think of is to re-flash on an api call, but that seems weird. Any other way?

Thanks

0 likes
1 reply
renedekat's avatar
Level 14

@danWilcz It isn't weird that you have to re-flash them. Laravel doesn't know that you don't want to use your flash messages for AJAX results. A flash message disappears upon the next request from the same host, whether that is an AJAX request or not. So, if you want to retain them, re-flash them. Easy enough:

Session::reflash();

Or if you only want to keep a subset:

Session::keep(['username', 'email']);

1 like

Please or to participate in this conversation.