Hi, I just want to understand for whati this 'cart ' refer to in the session , i mean if i changed it to 'cartttt' or anything else is this will effect and what it will effect , I really i don't understand the sessions here
public function addToCart(Product $product) {
if (session()->has('cart')) {
$cart = new Cart(session()->get('cart'));
} else {
$cart = new Cart();
}
$cart->add($product);
//dd($cart);
session()->put('cart', $cart);
return redirect()->route('product.index')->with('success', 'Product was added');
}
Cart is a session variable with the ability to add items into your users session's cart. If you change it (i.e. everywhere it's used), the existing users with cart's will be empty as your using a different variable.
Sessions are a way to store information about the user across multiple requests
No. You could call it anything basket, shopping_cart, bucket, etc. Whatever you call it is the key into the Session with the value being the contents of the Cart, so wherever the Cart (and its contents) are created, read, updated, and destroyed should be using the same key.
Thank but i have problem with understanding the if session() ,, Is it condition to do what i am very sorry but i really want to understand it 100% with simple way
if(session()->has('cart')) is exactly how it reads, i.e. if the Session has a cart then $cart gets the contents of the cart from the session, otherwise, a new empty Cart is created.