【问题标题】:Undefined property: App\Cart::$totalPrice未定义的属性:App\Cart::$totalPrice
【发布时间】:2020-01-24 04:34:42
【问题描述】:

您好,我正在制作购物车,但是当我点击添加到购物车链接时,它会显示:

未定义属性:App\Cart::$totalPrice

错误:https://ibb.co/ysB5CfG

型号:

  <?php

  namespace App;

  use Illuminate\Database\Eloquent\Model;

  class Cart
  {
private $contents;
private $totalQty;
private $contentsPrice;

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

public function addProduct($product, $qty){
    $products = ['qty' => 0, 'price' => $product->price, 'product' => $product];
    if ($this->contents) {
        if (array_key_exists($product->slug, $this->contents)) {
            $product = $this->contents[$product->slug];
        }
    }

    $products['qty'] +=$qty;
    $products['price'] +=$product->price * $product['qty'];
    $this->contents[$product->slug] = $product;
    $this->totalQty+=$qty;
    $this->totalPrice += $product->price;
}

public function getContents()
{
    return $this->contents;
}
public function getTotalQty()
{
    return $this->totalQty;
}
public function getTotalPrice()
{
    return $this->totalPrice;
}
}

控制器:

   public function cart()
{
    if (!Session::has('cart')) {
        return view('products.cart');
    }
    $cart = Session::has('cart');
    return view('product.cart', compact('cart'));
}

public function addToCart(Product $product, Request $request, $qty= null)
{
    if(empty(Auth::user()->email)){
        $data['email'] = '';    
    }else{
        $data['email'] = Auth::user()->email;
    }

    $oldCart = Session::has('cart') ? Session::get('cart') : null;
    $qty = $request->qty ? $request->qty : 1;
    $cart = new Cart($oldCart);
    $cart->addProduct($product, $qty);
    Session::put('cart', $cart);

    return redirect()->back()->with('flash_message_success', 'Product $product->title has been successfully added to Cart');
  }

路线:

  Route::get('cart', 'Admin\ProductController@cart')->name('product.cart');

  // Add to cart
  Route::get('/addToCart/{product}/{qty?}', 'Admin\ProductController@addToCart')->name('addToCart');

【问题讨论】:

  • 在你的模型文件中定义一个私有类private $contentsPrice;。但不定义private $totalPrice。所以我认为这是错误的。
  • @AmitSenjaliya thnx 但现在它没有重定向到购物车页面,它只重新加载产品详细信息页面,意味着它刷新了自己的页面
  • 我认为您应该使用return redirect()-&gt;route('product.cart'); 而不是在控制器文件中重定向回。但是您可以检查产品是否已成功添加到购物车中?
  • 我已经在购物车页面写了这个 if(isset($cart) && $cart->getContents()) //other code else

    没有产品在购物车中 购买一些产品

    endif
  • 这就是为什么我告诉你检查是否添加到购物车中的产品。

标签: laravel eloquent laravel-5.8


【解决方案1】:

您应该在控制器文件的 cart() 函数中使用 get() 方法。

public function cart()
{
  if (!Session::has('cart')) {
    return view('products.cart');
  }
  $cart = Session::get('cart');
  return view('product.cart', compact('cart'));
}

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 2021-09-26
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多