2017-05-24 59 views
0

我的數據庫查詢有問題。
我第一次導入了兩個條目,如THIS,並且數據插入正確。如何在數據存在時更新數據,如果不存在則不創建數據,Laravel 5

wholesaler_id |目標|周| total_transaction |回扣| total_voucher

11223344  | 100.000| 1.2017| 50.000   | 2,25 | 0 
11223344  | 100.000| 2.2017| 120.000   | 2,25 | 2700 
11223344  | 100.000| 3.2017| 185.000   | 2,25 | 1462,5 
11223344  | 100.000| 4.2017| 248.000   | 2,25 | 1417,5 

但是,當我與其他行enter image description here再次導入,結果如下:

wholesaler_id |目標|周| total_transaction |回扣| total_voucher

11223344  | 100.000| 1.2017| 50.000   | 2,25 | 0 
11223344  | 100.000| 2.2017| 120.000   | 2,25 | 2700 
11223344  | 100.000| 3.2017| 185.000   | 2,25 | 1462,5 
11223344  | 100.000| 4.2017| 248.000   | 2,25 | 1417,5 
11223344  | 100.000| 1.2017| 63.100   | 2,25 | 0 
11223344  | 100.000| 2.2017| 142.700   | 2,25 | 2700 
11223344  | 100.000| 3.2017| 205.000   | 2,25 | 1462,5 
11223344  | 100.000| 4.2017| 279.400   | 2,25 | 1417,5 

我想要的結果如下:

 
wholesaler_id | target | week | total_transaction | rebate | total_voucher 

11223344  | 100.000| 1.2017| 63.100   | 2,25 | 0 
11223344  | 100.000| 2.2017| 155.800   | 2,25 | 2700 
11223344  | 100.000| 3.2017| 240.800   | 2,25 | 1462,5 
11223344  | 100.000| 4.2017| 332.200   | 2,25 | 1417,5

的回扣,總憑證列是沒有問題的,主要問題是在total_transaction
這是在我的控制器功能代碼importCsv

 


    $voucher = Voucher::firstOrCreate(array(
    'wholesaler_id' => $wholesaler_id, 
    'target' => $target, 
    'week' => $week . '.' . date("Y"), 
    'total_transaction' => $sum, 
    'rebate' => $wholesaler_type->rebate_percentage, 
    'total_voucher' => $total_voucher 
    )); 

回答

0

應該這樣做..

$voucher = Voucher::firstOrCreate(['wholesaler_id'=>$wholesaler_id], 
    [ 
     'target' => $target, 
     'week' => $week . '.' . date("Y"), 
     'total_transaction' => $sum, 
     'rebate' => $wholesaler_type->rebate_percentage, 
     'total_voucher' => $total_voucher 
    ] 
); 
相關問題