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

calin.ionut's avatar

update Session on every page reload

I am using laavel 5.8 and I want to run this code on every request on the server:

if(Session::has('cart')){
	Session::put('cart', new Cart(Session::get('cart')));
}

I have tried in AppServiceProvider in boot .... but not working (.... it seems that Session is not loading in the boot....)

I need to check and update some fields on every page load....

Anyone has any idea ???

0 likes
8 replies
MichalOravec's avatar

Use middleware for that. But why do want update it on every page load?

calin.ionut's avatar

@michaloravec Why? I have some fields that need to be calculated only if the users is logged in... and other conditions :)

calin.ionut's avatar

so....if I create a middleware.....I have to run it on every Controller ?

$this->middleware('checkCart');

calin.ionut's avatar

@michaloravec ..... i need it on almost every route....because the cart content is loading on every view in the header.

jlrdw's avatar

Typically you would have a View Cart and an edit cart link. This is where the session data would come in to display the current cart contents.

For example when user continues to shop, you don't need that session data at that point.

In this application if you are using any Ajax, it would not work the same.

Please or to participate in this conversation.