I want to collect product and push to the session cart.
$product= Product::find(1);
$product2= Product::find(2);
session(['cart' => collect($product)]);
session(['cart' => session('cart')->push($product2)])
This is my output with some dummy data.
{
"0": {
"id": 2,
"sku": "SKU914",
"price": 1.0,
"special_price": 0.91,
"weight": 763,
"barcode": "0434120288572",
"created_at": "2020-01-31 14:39:53",
"updated_at": "2020-01-31 14:39:53"
},
"id": 1,
"sku": "SKU579",
"price": 22.8,
"special_price": 19.14,
"weight": 478459,
"barcode": "1390377688",
"created_at": "2020-01-31 14:34:59",
"updated_at": "2020-01-31 14:34:59"
}
The second product ($product2) which I pushed become outside of the array.
How should I do to make it like "0": {}, "1": {}
Just place $product in an array.
session(['cart' => collect([$product])]);
session(['cart' => session('cart')->push($product2)]);
Or just push
session(['cart' => session('cart')->push($product)]);
session(['cart' => session('cart')->push($product2)]);