【发布时间】:2017-10-09 07:32:04
【问题描述】:
这是我的密码
$filter = DB::table('detail_clothes')
->get(['id', 'clothes_detail_date', 'clothes_price'])
->groupBy(function($date){
return Carbon::parse($date->clothes_date)->format('m/Y');
});
$result = [];
$clothes_total_price = 0;
$clothes_date = null;
foreach ($filter as $dn => $dn_value) {
$clothes = Clothes::where('clothes_date', $dn)
->first();
foreach ($dn_value as $k => $dnval) {
$clothes_total_price += $dnval->clothes_price;
$result[$dn]['clothes_total_price'] = $clothes_total_price;
}
$date_temp = date_format(date_create_from_format('Y-m-d', $request->clothes_detail_date),'m/Y');
}
我有两个模型:衣服和细节衣服
Clothes : id, clothes_date, clothes_total_price
DetailClothes : id, clothes_detail_date, clothes_price
示例:: 当我输入衬衫价格时,它会进入详细的衣服并将其存储在那里,它也会存储在衣服中作为衣服总价格
它会根据月份显示所有记录,但是当我总结它时,它不会根据我想要的显示,
我想要的例如: 首先,如果我本月输入两次1000的价格,总价应该是2000,如果我输入两次下个月的价格1000,总价应该是2000而不是4000
其次,如果我输入日期示例: 2017-08-25 ,它将存储到两个模型,但特别是对于 CLOTHES 模型,它总是会根据月份和年份将日期更新为最新提交的日期,
example:
at Detail Clothes model it should be like this::
1st submit : clothes_detail_date : 2017-08-25, clothes_price : 1000
2nd submit : clothes_detail_date : 2017-08-01, clothes_price : 2000,
expected result:
at Clothes model it should be like this::
clothes_date : 2017-08-25, clothes_total_price: 3000
注意* 在 Clothes Model 中,它只会根据月份和年份显示 1 行记录,并且永远不会在同一月份和年份显示 2 条记录
谁能帮帮我??????
【问题讨论】:
-
您希望如何通过循环在 PHP 中获得结果或直接从查询中获得结果?
-
另外,您能否给出示例表和数据的 SQL,以便我们可以尝试使用 SQLFiddle?
-
@DhavalPurohit 我是新手,所以我真的不明白它是如何工作的,
-
@wast 等我更新问题
-
@DhavalPurohit 查询的直接结果怎么样