调用未定义的方法App \ Product :: getProductStock()

时间:2019-10-28 12:34:40

标签: laravel laravel-5 eloquent

  

BadMethodCallException   调用未定义的方法App \ Product :: getProductStock()   在控制器中访问模型功能时发生此错误

我的Controller


            $user_id = Auth::user()->id;
            $user_email = Auth::user()->email;


            // To prevent out of stock material from ordering
            $userCart=DB::table('cart')->where('user_email',$user_email)->get();
            // echo "<pre>"; print_r($userCart); die;
            foreach ($userCart as $cart) {

                $product_stock= Product:: getProductStock($cart->product_id,$cart->size);
                echo $product_stock;

                if ($product_stock==0) {
                    return redirect('/cart')->with('flash_message_error','Product is Stock sold out. Buy another product');
                }

                if ($Cart->quantity>$product_stock) {
                    return redirect('/cart')->with('flash_message_error','Reduced product stock & Try Again');
                }

            }

Model

    public static function getProductStock($product_id,$product_size)
    {
        $getProductStock=ProductsAttribute::select('stock')->where(['product_id'=>$product_id,'size'=>$product_size])->first();
        return $getProductStock->stock;
    }

1 个答案:

答案 0 :(得分:0)

我认为您的产品检索方法不是最好的。

要获取产品,您应该使用以下方法:

setActiveTabId(currentTabId)

取回股票应该是:

$product = Product::find($cart->product_id);