简体   繁体   中英

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 . So I need help on how to convert such to 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 .

I think what you will be using is session()->has() and session()->push() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM