为什么我的$ item不包含一组$ item?Laravel 8 电子商务 [英] Why is my $items doesn't hold a group of $item? Laravel 8 Ecommerce

查看:45
本文介绍了为什么我的$ item不包含一组$ item?Laravel 8 电子商务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自同一篇帖子,但是我发现了根本问题,为什么我的数量不会增加,但是我不知道如何解决这个问题.问题出在我的isset函数上(在Cart.php中),因为我试图在该函数中回显某些东西,只是意识到该函数未运行.这是我在if($ this-> items)删除isset函数时弹出的错误.我也尝试dd($ items),它说为空.为什么我的$ item不持有一组$ item?你们知道为什么以及如何解决这个问题吗?

I'm from the same post here but I've found the root problem why is my quantity won't increment but I don't know how to solve this. The problem is at my isset function (in Cart.php), because I tried to echo something there and just realized the function isn't running. This is the error pops up when I removed isset function at the if($this->items). I tried to dd($items) too, and it said null. Why is my $items isn't holding a group of $item? Do you guys know why and how to solve this?

p/s:从上一篇文章中,我已经删除了我的$ oldCart并用$ cart替换了它,并且我希望$ cart能够覆盖自己.这些都是与我的购物车有关的代码.

p/s: from the previous post, I've removed my $oldCart and replace it with $cart, and I'm expecting $cart to overwrite itself. These are all the codes related to my shopping cart.

在Cart.php中

<?php

namespace App\Models;

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


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


    public function add($item, $id)
    {
        global $cart;
        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 ($this->$items) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }

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

在FoodController.php

in FoodController.php

<?php

namespace App\Http\Controllers;

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


class FoodController extends Controller
{
public function addtoCart(Request $request, $id, Food $foods)
    {

        $foods = Food::find($id);

        //check in session if cart already contains product
        $cart = Session::has('cart') ? Session::get('cart') : null;

        //dd($cart);
        //if it did contain products, pass them to constructor
        if (!$cart) {
            $cart = new Cart($cart);
        }

        $food = $foods->id;
        $cart->add($foods, $food);

        Session::put('cart', $cart);
        //dd($request->session()->get('cart'));


        return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
    }

    public function getCart()
    {
        if (!Session::has('cart')) {
            return view('cart');
        }

        $cart = Session::get('cart');
        $cart = new Cart($cart);
        return view('cart', ['food' => $cart->items, 'totalPrice' => $cart->totalPrice]);
    }
}

在cart.blade.php中

in cart.blade.php

    @foreach ($food as $foods)
                        
                        
       <p align="left"><a href="#"> {{ $foods['item']['name'] }} </a> <span class="price">MYR {{ $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>
                        
<p align="left">Total <span class="price"><b>MYR {{ $totalPrice }}</b></span></p>

推荐答案

我认为您需要在Cart类中重新定义您的add函数,因为这样,存在一些语法错误,(删除 $ 签署表单 $ this-> $ items ),则无需重新声明$ cart&函数中的$ items

I think you need to re-define your add function in the Cart class to be like this, there are some syntax errors, (remove $ sign form $this->$items), and there is no need to re-declare $cart & $items in the function

public function add($item, $id)
    {
        //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) ? $this->items : false) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }

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

这篇关于为什么我的$ item不包含一组$ item?Laravel 8 电子商务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆