第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

此集合實(shí)例上不存在屬性 [stock]

此集合實(shí)例上不存在屬性 [stock]

PHP
largeQ 2023-08-19 16:08:43
在我的購(gòu)物車模型中,我與產(chǎn)品模型有關(guān)系。在產(chǎn)品模型中,我與Product_attribute模型有關(guān)系。但我與Product_attribute模型沒(méi)有關(guān)系。在本例中,我編寫(xiě)此代碼來(lái)訪問(wèn)Product_attribute模型。$getCartDetails = Cart::with('product.product_attribute')->where('id',$id)->first();工作正常。我訪問(wèn)產(chǎn)品和產(chǎn)品屬性數(shù)據(jù)現(xiàn)在我想統(tǒng)計(jì)product_attribute 庫(kù)存,類似的方式想統(tǒng)計(jì)購(gòu)物車 數(shù)量。如果Product_attribute->stock >= Cart->quantity,我更新購(gòu)物車表,否則不更新這就是我寫(xiě)這段代碼的原因   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 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以直接通過(guò)以下方式檢查

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

$check_qty 將根據(jù)購(gòu)物車表 Product_id 和 Size 為您提供庫(kù)存數(shù)量。使用 If 條件檢查更新的數(shù)量和庫(kù)存數(shù)量后。

因?yàn)槟梢愿鶕?jù)product_id和Size從product_attribues表中找到產(chǎn)品庫(kù)存


查看完整回答
反對(duì) 回復(fù) 2023-08-19
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

看看錯(cuò)誤,它說(shuō)property stock doesn't exist on the collection instance.

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

編輯:

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

查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 2 回答
  • 0 關(guān)注
  • 144 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)