private function calculateAmount($productIds) { $this->amount = 0; foreach ($productIds as $id) { $this->amount += Product::findOrFail($id)->price; } return $this->amount; }
/** * 顯示結帳頁面 */ public function index() { // 建立客戶 Account $account = Account::updateOrCreate(['id' => 1], ['name' => "梅宗主"]); $account->save(); // 模擬加入購物車動作 $cart = new MyCart(); $cart->add(Product::updateOrCreate(['id' => 1], ['name' => '暖爐', 'price' => '4000'])); $cart->add(Product::updateOrCreate(['id' => 2], ['name' => '護心丹', 'price' => '12000'])); $cart->add(Product::updateOrCreate(['id' => 3], ['name' => '密道鐵門', 'price' => '8000'])); return view('checkout', ['cart' => $cart, 'account' => $account]); }