简体   繁体   English

Laravel 添加到购物车问题

[英]Laravel add to cart issue

Trying to writer add to cart script in laravel.尝试在 laravel 中编写添加到购物车脚本。 Created a class called cart, Created a product controller where i get product details with product name, quantity, price using an array additem创建了一个名为购物车的 class,创建了一个产品 controller,在其中我使用数组 additem 获取产品名称、数量、价格的产品详细信息

class Cart
{
    public $items;
    public $totalQuantity;
    public $totalPrice;

    public function __construct($prevCart)
    {
        if ($prevCart != null) {
            $this->items = $prevCart->items;
            $this->totalQuantity = $prevCart->totalQuantity;
            $this->totalPrice = $prevCart->totalPrice;
        } else {
            $this->items = [];
            $this->totalQuantity = 0;
            $this->totalPrice = 0;
        }
    }

    public function additem($id, $product)
    {
        $price = (int)str_replace('$', '', $product->price)
        //item already exists
        if (array_key_exists($id, $this->items)) {
            $productToAdd = $this->items[$id];
            $productToAdd['quantity']++;
        } else {
            $productToAdd = ['quantity' => 1, 'price' => $price, 'date' => $product];
        }

        $this->items[$id] = $productToAdd;
        $this->totalQuantity++;
        $this->totalPrice = $this->totalPrice + $price;
    }
}

It shows the following error:它显示以下错误:

syntax error, unexpected 'if' (T_IF)语法错误,意外的 'if' (T_IF)

you are missing a closing ';'你错过了一个结束';' in this row:在这一行:

$price= (int) str_replace('$','',$product->price)

the 'unexpected' error in php should always remind you to check for it. php 中的“意外”错误应始终提醒您检查它。 hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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