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

时间:2018-11-18 16:40:21

标签: php laravel

Product Table

Product Attribute Table

我的PostOrder函数

        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');
      }
我有一个存储产品详细信息的表“产品”和一个存储产品库存的表“ products_attribute”。我不知道如何将其链接到products_attribute表。请帮忙。

2 个答案:

答案 0 :(得分:0)

您应该使用关系here

首先创建ProductModel 之后,您创建一种类似属性的方法 并使用hasMany关系 所以你可以做任何你想做的事

答案 1 :(得分:0)

您似乎已经对EloquentOrder模型的DeliveryAddress模型有所了解。

要在ProductProduct Attribute之间建立关系,只需在Product模型内部定义关系方法:

<?php

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function attributes()
    {
        return $this->hasMany('ProductAttribute');
    }
}

Eloquent将使用蛇形大小写来确定外键的名称,在这种情况下,外键的名称为product_id。如果您的外键定义不同,则可以将名称指定为第二个参数:

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

如果您希望从ProductAttributeProduct之间建立联系,请使用belongsTo方法:

<?php

use Illuminate\Database\Eloquent\Model;

class ProductAttribute extends Model
{
    public function product()
    {
        return $this->belongsTo('Product');
    }
}

您还可以指定类似于hasMany方法的自定义外键名称:

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

要更新质量,您可以执行以下操作:

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();
        }
    }