Laravel 8 购物车项目数量不会增加

时间:2020-12-28 06:26:18

标签: php laravel laravel-5 e-commerce laravel-8

新更新:我已经解决了这个问题,你可以参考这个post来寻找答案。

我是 laravel 的初学者,我一直在尝试创建一个简单的电子商务网站,但我在这个逻辑错误中陷入了好几天。我一直在关注有关创建购物车的 youtube 教程,但如果我在购物车中添加相同的项目,项目数量不会增加。虽然 totalPrice 和 totalQty 看起来不错...
这是我尝试dd($this)进入cart.php时得到的。

我认为这是因为我在 add 函数(在 Cart.php 中)中声明了数组,并且它不断覆盖当前数量。但我不能在函数之外声明它,否则它会抛出一个错误,指出未定义的索引。我试过在这里检查一些相关的帖子,但它仍然没有用。我不知道该怎么办了。
各位大佬知道有什么解决办法吗?谢谢!

p/s:这是我观看的视频教程链接。感谢 Academind 提供的精彩教程。

https://www.youtube.com/watch?v=4J939dDUH4M&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH&index=9

这些是我与错误相关的代码。

在 Cart.php 中

<?php

namespace App\Models;

class Cart
{
    public $items = array();
    public $totalQty = 0;
    public $totalPrice = 0;


    public function __construct($oldCart)
    {
        if ($oldCart) {
            //dd($this);
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }


    public function add($item, $id)
    {
        //force get $items
        global $items;

        //default values if item not in cart
        $storedItem = [
            'qty' => 0,
            'price' => $item->price,
            'item' => $item
        ];
        
        //if item is already in shopping cart
        if (isset($this->$items) ?? $items::false) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }


        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;
        $this->totalQty++;
        $this->totalPrice += $item->price;
        dd($this);
    }
}

在 FoodController.php 中

 <?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Food;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

public function addtoCart(Request $request, $id, Food $foods)
        {
    
            $foods = Food::find($id);
            //check in session if cart already contains product
            $oldCart = Session::has('cart') ? Session::get('cart') : null;

            //if it did contain products, pass them to constructor
            $cart = new Cart($oldCart);
    
            $cart->add($foods, $foods->id);
    
            $request->session()->put('cart', $cart);
    
    
            return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
        }
    
    public function getCart()
    {
        if (!Session::has('cart')) {
            return view('cart', ['food' => null]);
        }
    
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);
        return view('cart', ['food' => $cart->items,'totalPrice' =>$cart->totalPrice]);
        }

在cart.blade.php

@foreach ($food as $foods)
                    
                    
    <p align="left"><a href="#"> {{ $foods['item']['name'] }} </a> <span class = "price"> ${{ $foods['price'] }} </span></p>
    <p align="left">Quantity</p>
    <p><input class="w3-input w3-p-adding-16 w3-border" type="number" placeholder="Quantity" value="{{ $foods['qty'] }}" required name="quantity"></p>
                   
@endforeach
                    
                    
<hr class="new1">
<p align="left">Total <span class="price"><b> ${{ $totalPrice }} </b></span></p>

0 个答案:

没有答案