php - 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: []
}
如何对关系表的值求和,然后按总和对整个库存进行排序?
解决方案
你可以使用一些东西,比如......
在控制器中
return $inventory->append('deliverySum');
现在在库存模型中
public function getDeliverySumAttribute()
{
$sum = 0;
$this->deliverRecord->each(function (DeliverRecord $deliverRecord)use(&$sum) {
$sum += $deliverRecord->sum;
});
return $sum;
}
推荐阅读
- math - 找到要放置在给定半径的球体中的最大圆柱体
- sql - SQL Server Alert on Missing Stored Procedure
- python - python not excecuting if statement
- c# - I want to make enemies look at player when they spawn in
- python - 如何在不下载 mp3 的情况下将音频从 pytube 流式传输到 discord.py?
- vb.net - 当 2 个用户使用同一个程序时出现问题
- javascript - 如何访问 div 中的图像属性?jQuery
- selenium - Google 搜索按钮的元素名称是什么?
- javascript - 如何将 JavaScript 中的主机名和布尔值与 if/else 进行比较以在 Mongo Shell 中执行?
- python - 如何使用循环来减少python中的代码