dpblwmh5218 2018-11-18 16:40
浏览 75
已采纳

如何在Laravel PHP中下订单后更新产品数量

Product Table

Product Attribute Table

My PostOrder Function

        public function getPaymentStatus(Request $request){

        if ($result->getState() == 'approved') {
        $user_id = Auth::user()->id;
        $user_email = Auth::user()->email;

        //get shipping details
        $shippingDetails = DeliveryAddress::where(['user_email'=>$user_email])->first();

        $order = new Order;
        $order->user_id = $user_id;
        $order->user_email = $user_email;
        $order->name = $shippingDetails->name;
        $order->address = $shippingDetails->address;
        $order->city = $shippingDetails->city;
        $order->state = $shippingDetails->state;
        $order->country = $shippingDetails->country;
        $order->postcode = $shippingDetails->postcode;
        $order->mobile = $shippingDetails->mobile;
        $order->coupon_code = $coupon_code;
        $order->coupon_amount = $coupon_amount;
        $order->order_status = "New";
        $order->grand_total = Session::get('grand_total');
        $order->save();

        $order_id = DB::getPdo()->lastInsertId();

        $cartProducts = DB::table('cart')->where(['user_email'=>$user_email])->get();
        foreach($cartProducts as $pro){
            $cartPro = new OrdersProduct;
            $cartPro->order_id = $order_id;
            $cartPro->user_id = $user_id;
            $cartPro->product_id = $pro->product_id;
            $cartPro->product_code = $pro->product_code;
            $cartPro->product_name = $pro->product_name;
            $cartPro->product_size = $pro->size;
            $cartPro->product_color =  $pro->product_color;
            $cartPro->product_price = $pro->price;
            $cartPro->product_qty = $pro->quantity;
            $cartPro->save();
        }
        return redirect('/thank-you');
      }

i have a table "products" which store product details and a table "products_attribute" which store the stock of the product. I don't know how link this to products_attribute table. Please Help.

  • 写回答

2条回答 默认 最新

  • douhuigan8063 2018-11-18 17:29
    关注

    It looks like you already have a grasp of Eloquent Models with your Order and DeliveryAddress model.

    To setup relationships between your Product and Product Attribute simply define the relationship method inside your Product model:

    <?php
    
    use Illuminate\Database\Eloquent\Model;
    
    class Product extends Model
    {
        public function attributes()
        {
            return $this->hasMany('ProductAttribute');
        }
    }
    

    Eloquent will use snake case in determining the name of foreign key, which in this case would be product_id. If your foreign key is defined differently you may specify the name as a second argument:

    return $this->hasMany('ProductAttribute', 'my_product_id');
    

    If you want to have a relationship from ProductAttribute to Product use the belongsTo method:

    <?php
    
    use Illuminate\Database\Eloquent\Model;
    
    class ProductAttribute extends Model
    {
        public function product()
        {
            return $this->belongsTo('Product');
        }
    }
    

    You may also specify a custom foreign key name similar to the hasMany method:

    return $this->belongsTo('Product', 'my_product_id');
    

    To update the quauntity you could do something like this:

    foreach($cartProducts as $pro){
            $cartPro = new OrdersProduct;
            $cartPro->order_id = $order_id;
            $cartPro->user_id = $user_id;
            $cartPro->product_id = $pro->product_id;
            $cartPro->product_code = $pro->product_code;
            $cartPro->product_name = $pro->product_name;
            $cartPro->product_size = $pro->size;
            $cartPro->product_color =  $pro->product_color;
            $cartPro->product_price = $pro->price;
            $cartPro->product_qty = $pro->quantity;
            $cartPro->save();
    
            $product = Product::find($pro->product_id);
    
            if (null !== $product) {
                // you will need a sku or some other unique identifier
                // from your attribute to determine the correct attribute
                // to modify after your order is complete
                $attribute = $product->attributes()-where('sku', $sku)->first();
                $attribute->quantity = $attribute->quantity - $pro->quantity;
                $attribute->save();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?