[英]How to use $_SESSION in laravel to add items to cart
I would love to use the above code in Laravel the same way I use that $_SESSION
in normal php
.我很想在 Laravel 中使用上面的代码,就像在普通php
中使用$_SESSION
一样。 So I need help on how to convert such to Laravel.所以我需要有关如何将其转换为 Laravel 的帮助。 I am only try to add stuff to cart.我只是尝试将东西添加到购物车。
if(isset($_POST["action"]))
{
if($_POST["action"] == "add")
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$is_available++;
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
}
}
if($is_available == 0)
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
}
Please help me to convert.请帮我转换。
Have a look at the Laravel session documentation .查看Laravel session 文档。
I think what you will be using is session()->has()
and session()->push()
.我认为您将使用的是session()->has()
和session()->push()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.