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条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器