Trying to writer add to cart script in 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 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)
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. hope this helps:)
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.