2015-12-30 62 views
1

所以,我有表prices與結構:得到的所有條目的總和

+----+--------+ 
| id | reward | 
+----+--------+ 
| 1 | 721 | 
+----+--------+ 
| 2 | 54  | 
+----+--------+ 
| 3 | 99  | 
+----+--------+ 

,我用這個方法來概括所有獎勵:

'withdrawals' => \App\Tradeoffer::where('type', 'withdraw') 
      ->where('completed', 1) 
      ->where('declined', 0) 
      ->where('timeout', 0) 
      ->where('created_at', '>', (time() - $hours)) 
      ->sum('reward') 

和響應:7215499而不是所有條目的總和。這是爲什麼?如何處理它?

+0

嗯,這些東西是字符串還是數字? (1) – Roope

+0

列爲int(11) –

回答

0

你應該做的是確保讓您rewardprices表中的數據庫是數字(整數/浮點),而不是varchar

+0

列爲int(11) –

+1

當您在MYSQL中運行時會得到什麼結果:'SELECT sum(reward)FROM prices WHERE type ='withdraw'AND completed = 1 AND declined = 0 AND timeout = 0'? –

+0

@ArnasPecelis在哪張表中驗證了它是int(11)?您的'價格'表格結構不同於'Tradeoffer' –

1

我認爲你可以不喜歡它,

$totalReward = \App\Tradeoffer::selectRaw('SUM(reward) as total') 
     ->where('type', 'withdraw') 
     ->where('completed', 1) 
     ->where('declined', 0) 
     ->where('timeout', 0) 
     ->where('created_at', '>', (time() - $hours)) 
     ->first(); 

$totalReward->total; 
+0

看起來很不錯,但結果仍然是相同的...不算數.. –

0

代替使用我使用查詢生成器的模型,它工作(與我的數據庫)

DB::table('Tradeoffer') // Here is the name of the table 
    ->where('type', 'withdraw') 
    ->where('completed', 1) 
    ->where('declined', 0) 
    ->where('timeout', 0) 
    ->where('created_at', '>', (time() - $hours)) 
    ->sum('reward'); 

試試吧!