While this is not a strictly Laravel solution, it is a relatively simple and well supported (browser compatible) way to store simple session data on the client side. I do this using sessionStorage. Take a look at the sessionStorage documentation here.
Example JS:
// Save data to sessionStorage
sessionStorage.setItem('cart', 'item_id');
// Get saved data from sessionStorage
let data = sessionStorage.getItem('cart');
// Remove saved data from sessionStorage
sessionStorage.removeItem('cart');
// Remove all saved data from sessionStorage
sessionStorage.clear();
A shopping cart would usually store several different items and while sessionStorage does not store arrays, the workaround is to convert the comma seperated string to an array using split(",")
.
Example:
//if cart has value of 11,12,13,14,15
var ex1 = sessionStorage.getItem('cart').split(',');
//ex1 would be [11,12,13,14,15]
//without split(',')
var ex2 = sessionStorage.getItem('cart');
//ex2 would be 11,12,13,14,15