英文:
how to calculate the total of an order by multiplying the item price with it's quantity using laravel api?
问题
我正在使用Postman发送一个POST请求(作为测试),其中包含以下JSON格式:
{
"place_id": 2,
"items": [
{
"id": 9,
"quantity": 5
},
{
"id": 8,
"quantity": 3
},
{
"id": 5,
"quantity": 2
}
]
}
现在,在客户确认订单之前,我需要进行一些计算。每个项目都有自己的价格,我想计算每个项目的总价(价格 * 数量),但我对如何做到这一点感到有点困惑。
这是我的代码(尝试)。它给我返回了价格的总和,但我无法弄清楚如何计算每个项目的(价格 * 数量):
$items = $request->input('items',[]);
$item_array = [];
foreach ($items as $key=>$item)
{
$item_array[$key] = $item['id'];
}
$itemsPriceTotal = Item::whereIn('id',$item_array)->sum('price');
$itemsPriceTotal = (int)($itemsPriceTotal);
英文:
I'm sending a post request using post man(as a test) which contains the following json format:
{
"place_id": 2,
"items": [
{
"id": 9,
"quantity": 5
},
{
"id": 8,
"quantity": 3
},
{
"id": 5,
"quantity": 2
}
]
}
Now, before the customer confirms his order, I need to make some calculations. Each item has its own price, and I want to calculate the total of each item (price * quantity) but I got a little bit confused on how to do that.
This is my Code (attempt). it gave me the sum of prices But couldn't figure out how to calculate each item's (price * quantity):
$items = $request->input('items',[]);
$item_array = [];
foreach ($items as $key=>$item)
{
$item_array[$key] = $item['id'];
}
$itemsPriceTotal = Item::whereIN('id',$item_array)->sum('price');
$itemsPriceTotal =(int)($itemsPriceTotal);
答案1
得分: 3
$reqItems = $request->input('items');
// associative array of item ids and their respective quantities
$itemIdsQtys = [];
foreach($reqItems as $reqItem) {
$itemIdsQtys[$reqItem['id']] = $reqItem['quantity'];
}
// get array of item ids and fetch their respective models
$ids = array_keys($itemIdsQtys);
$itemModels = Item::find($ids);
// calculate total
$priceTotal = 0;
foreach($itemModels as $model) {
$subtotal = $model->price * $itemIdsQtys[$model->id];
$priceTotal += $subtotal;
}
return $priceTotal;
英文:
$reqItems = $request->input('items');
// associative array of item ids and their respective quantities
$itemIdsQtys = [];
foreach($reqItems as $reqItem) {
$itemIdsQtys[$reqItem['id']] = $reqItem['quantity'];
}
// get array of item ids and fetch their respective models
$ids = array_keys($itemIdsQtys);
$itemModels = Item::find($ids);
// calculate total
$priceTotal = 0;
foreach($itemModels as $model) {
$subtotal = $model->price * $itemIdsQtys[$model->id];
$priceTotal += $subtotal;
}
return $priceTotal;
答案2
得分: 0
我会利用模型的修改器。
请参阅 https://laravel.com/docs/5.8/eloquent-mutators:
/**
* 获取用户的全名。
*
* @return string
*/
public function getTotalCost()
{
// 示例:$qty = SomeModel::Find()->where(['id' => $this->id])->one()
return $this->qty * $this->price;
}
然后使用 $model->totalCost
。
英文:
I would leverage the model mutators.
See https://laravel.com/docs/5.8/eloquent-mutators:
/**
* Get the user's full name.
*
* @return string
*/
public function getTotalCost()
{
// Example: $qty = SomeModel::Find()->where(['id' =. $this->id])->one()
return $this->qty * $this->price;
}
Then use $model->totalCost
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论