2017-10-09 137 views
3

這是我的代碼如何獲得價格並根據日期總價作爲總價?

$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 

例如:: 當我輸入的襯衫價格將進入細節的衣服,並將其存儲在那裏,而且還保存在衣服clothes_total_price

它會根據月份顯示的所有記錄,但是當我總結它,它不按w ^顯示我想要的帽子,

我想要的例如: 首先,如果我在這個月輸入價格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行記錄,它會顯示w我永遠顯示在相同的月和年2記錄

誰能幫助我??????

+1

你怎麼想從查詢在PHP或直接導致循環,從而得到一個結果? –

+0

另外,你可以給SQL示例表和數據,所以我們可以嘗試使用SQLFiddle嗎? – wast

+0

@DhavalPurohit即時通訊新的在這,所以我真的不知道它是如何工作的, –

回答

0

我想你忘記了$ clothes_total_price重置爲零。此外,我從內部foreach移動到外部1行。

foreach ($filter as $dn => $dn_value) { 
    $clothes_total_price = 0; 
    $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'); 
} 

編輯:SQLFiddle附加應答

你組了一個月,採取從當月最大日期,併爲您總結的價格在一個月:

INSERT INTO 
    Clothes (clothes_date, clothes_total_price) 
    SELECT * FROM (
    SELECT 
     MAX(clothes_detail_date) new_clothes_date 
     ,SUM(clothes_price) new_clothes_total_price 
    FROM DetailClothes 
    GROUP BY DATE_FORMAT(clothes_detail_date,'%Y%m') 
) newTable 
ON DUPLICATE KEY UPDATE  
clothes_date=newTable.new_clothes_date 
,clothes_total_price=newTable.new_clothes_total_price 
; 
+0

感謝您的回答@wast, 第二個問題是如何是否存在,如果別人創造我所提到的,如何存儲或更新更新? –

+0

請更新它的問題,並提供樣本數據和你期望得到的。另外,如果這是正確的,則標記爲答案。 – wast

+0

我已經提到應該是什麼結果被@wast, –

0

我已經數字它出了如何回答這個問題

細節衣服模型

public function scopeStoreDetailClothes($query, $request){ 
    $data = $request->all(); 
    DetailClothes::create($data)->save(); 

    $date = Carbon::parse($request->clothes_detail_date); 

    $filter = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month) 
      ->whereYear('clothes_detail_date', '=', $date->year); 

    $total = (object) [ 
      'clothes_price' => $filter->sum('clothes_price'), 
    ]; 

    $detail_clothes = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month) 
      ->whereYear('clothes_detail_date', '=', $date->year) 
      ->orderBy('clothes_detail_date', 'desc') 
      ->first(); 

    $clothes = Clothes::whereMonth('clothes_date', '=', $date->month) 
      ->whereYear('clothes_date', '=', $date->month) 
      ->first(); 

    Clothes::updateOrCreate(
     [ 
      'clothes_date' => isset($clothes->clothes_date) ? $clothes->clothes_date : null 
     ], [ 
      'clothes_date' => isset($detail_clothes) ? $detail_clothes->clothes_detail_date : $request->clothes_detail_date, 
      'clothes_total_price' => $total->clothes_price 
     ]); 
}