首页 > 解决方案 > Laravel如何对相关表中的值求和并将值添加到对象并按总和值排序?

问题描述

我有两个表“inventory”和“deliver_record”。表关系是inventory->hasMany(deliver_record)。我需要获取 Deliver_record 单位的值并将它们相加,然后将总和添加到库存对象。在此之后,我们需要按库存对象中的总和值进行排序。

现在对象看起来像这样:

Inventory {#641 ▼
  #table: "inventory"
  #guarded: array:1 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:21 [▼
    "id" => 3
    "client_id" => 52
    "sku_number" => null
    "upc_number" => ""
    "product_number" => "217353"
    "other_product_numbers" => "BR0017"
    "brand" => "Britpart"
    "supplier" => "Bearmach"
    "storage_location" => "A2"
    "title" => "HUB LOCKING WASHER SERIES 1 - 111 - DEFENDER - RANGE ROVER CLASSIC."
    "images" => "a:1:{i:0;s:19:"217353-WJjyBPGK.jpg";}"
    "weight" => 0
    "weight_unit" => "lb"
    "height" => 0
    "width" => 0
    "length" => 0
    "dimension_unit" => "in"
    "stock_level" => 5
    "part_type" => "Genuine"
    "created_at" => "2017-06-21 13:29:11"
    "updated_at" => "2019-11-23 18:10:38"
  ]
  #original: array:21 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:3 [▼
    "deliveryRecord" => Collection {#958 ▼
      #items: array:5 [▼
        0 => DeliveryRecord {#972 ▼
          #table: "inventory_delivery_records"
          #guarded: array:1 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:11 [▼
            "id" => 4
            "client_id" => 0
            "product_id" => 3
            "price_original" => "0.14"
            "price_original_currency" => "GBP"
            "price" => "0.19"
            "price_currency" => "USD"
            "units" => 110
            "conv_rate" => "1.3500"
            "created_at" => "2018-08-23 23:00:00"
            "updated_at" => "2018-08-23 23:00:00"
          ]
          #original: array:11 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
        }
        1 => DeliveryRecord {#970 ▼
          #table: "inventory_delivery_records"
          #guarded: array:1 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:11 [▼
            "id" => 3616
            "client_id" => 0
            "product_id" => 3
            "price_original" => "0.15"
            "price_original_currency" => "GBP"
            "price" => "0.20"
            "price_currency" => "USD"
            "units" => 20
            "conv_rate" => "1.3100"
            "created_at" => "2019-10-20 00:42:19"
            "updated_at" => "2019-10-20 00:42:19"
          ]
          #original: array:11 [▶]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
        }
        2 => DeliveryRecord {#966 ▶}
        3 => DeliveryRecord {#968 ▶}
        4 => DeliveryRecord {#967 ▶}
      ]
    }
    "orderInRecord" => Collection {#1194 ▶}
    "sellRecord" => Collection {#1673 ▶}
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
}

如何对关系表的值求和,然后按总和对整个库存进行排序?

标签: phpmysqllaravel

解决方案


你可以使用一些东西,比如......

在控制器中

return $inventory->append('deliverySum');

现在在库存模型中

public function getDeliverySumAttribute()
{
    $sum = 0;
    $this->deliverRecord->each(function (DeliverRecord $deliverRecord)use(&$sum) {
        $sum +=   $deliverRecord->sum;
    });

    return $sum;
}

推荐阅读