php - create shopping cart - laravel 6

I want to create my own shopping cart
I would like to know what is the way to store various items and then show the items in the cart
public function __construct()
{
if(!\Session::has('cart')) \Session::put('cart', array());
}
my question is what code do I use to show the cart and then insert items inside it
Answer
Solution:
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 usingsplit(",")
.
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
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: regex stop at first match
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.