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 ???
Use middleware for that. But why do want update it on every page load?
@michaloravec Why? I have some fields that need to be calculated only if the users is logged in... and other conditions :)
so....if I create a middleware.....I have to run it on every Controller ?
$this->middleware('checkCart');
@michaloravec ..... i need it on almost every route....because the cart content is loading on every view in the header.
Just add them all inside a route group
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.