亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

此集合實例上不存在屬性 [stock]

此集合實例上不存在屬性 [stock]

PHP
largeQ 2023-08-19 16:08:43
在我的購物車模型中,我與產品模型有關系。在產品模型中,我與Product_attribute模型有關系。但我與Product_attribute模型沒有關系。在本例中,我編寫此代碼來訪問Product_attribute模型。$getCartDetails = Cart::with('product.product_attribute')->where('id',$id)->first();工作正常。我訪問產品和產品屬性數據現在我想統計product_attribute 庫存,類似的方式想統計購物車 數量。如果Product_attribute->stock >= Cart->quantity,我更新購物車表,否則不更新這就是我寫這段代碼的原因   public  function updateCartQuantity($id,$quantity)   {       $getCartDetails = Cart::with('product.product_attribute')->where('id',$id)->first();      if($getCartDetails->product->product_attribute->stock->sum() >= $getCartDetails->quantity->sum()) {           DB::table('carts')->where('id', $id)->increment('quantity', $quantity);           return back()->with('success', 'product Quantity in the cart Updated Successfully!');       }else{       return back()->with('error', 'Required Product Quantity is not available!');         }   }
查看完整描述

2 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

您可以直接通過以下方式檢查

$check_qty =    DB::table('product_attribues)->where('product_id',$getCartDetails->product_id)->where('size',$getCartDetails->size)->first();

$check_qty 將根據購物車表 Product_id 和 Size 為您提供庫存數量。使用 If 條件檢查更新的數量和庫存數量后。

因為您可以根據product_id和Size從product_attribues表中找到產品庫存


查看完整回答
反對 回復 2023-08-19
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

看看錯誤,它說property stock doesn't exist on the collection instance.

只需檢查一次關系即可。正如我從名稱中可以理解的那樣,該產品可能有多個與其關聯的產品屬性(One to many relation)。這可能是集合錯誤時該屬性不存在的原因。并且您正在嘗試訪問集合上模型的屬性,但情況不應該如此。

編輯:

$getCartDetails->product->product_attribute->sum('stock')?。這就是您的 if 條件所需要的。它將為您提供集合中所有實例的庫存屬性的總和。

查看完整回答
反對 回復 2023-08-19
  • 2 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號