0

I have this returned from two rows in my table, and I want to be able to display the sum in laravel blade.

["212,703.00","212,703.00"]

I have tried, but not working. the name of the column is amount

 $investamount = DB::table('investment')->get();
 return $investamount->sum('amount');
Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36
PPK
  • 41
  • 1
  • 9
  • Does this answer your question? [Laravel Eloquent Sum of relation's column](https://stackoverflow.com/questions/21679678/laravel-eloquent-sum-of-relations-column) – N69S Mar 06 '20 at 08:13
  • no it does not answer my question – PPK Mar 06 '20 at 08:19

5 Answers5

1

It seems you are using varchar for your amount field having comma then you can do it like this

DB::table('investment')->select("SUM(REPLACE(amount, ',', '')") as total)->get()
Sehdev
  • 5,486
  • 3
  • 11
  • 34
  • SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SUM(amount)' in 'field list – PPK Mar 06 '20 at 08:12
  • @PPK you can use the sum method directly – N69S Mar 06 '20 at 08:13
  • You don't need to use get() when using the aggregate method as sum. Use – Sehdev Mar 06 '20 at 08:13
  • Yes i tried this before but it gave me 424 as the sum for 212,703.00","212,703.00"...what happens to the 703, 703?? – PPK Mar 06 '20 at 08:15
  • are 703 stored on separate columns? – Sehdev Mar 06 '20 at 08:16
  • no same colume 212,703.00 times 2 is what i want..but its giving me 212 times 2 as result – PPK Mar 06 '20 at 08:17
  • @PPK 212,703.00 is a string format that is not a valid number. the sum just used what it could (212+212) and discarded the rest of the string. – N69S Mar 06 '20 at 08:20
  • How do i solve this problem? i need the answer of 212,703.00 + 212,703.00, but what i am getting is 424 as answer. – PPK Mar 06 '20 at 08:21
  • Use this `DB::table('investment')->select(SUM(REPLACE(amount, ',', '')) as total)->get()` – Sehdev Mar 06 '20 at 08:22
  • something is wrong with the code..not sure what it is – PPK Mar 06 '20 at 08:26
  • yes double quotes were missing. Try this `DB::table('investment')->select("SUM(REPLACE(amount, ',', '')") as total)->get()` This will give you result in single line – Sehdev Mar 06 '20 at 09:04
1

You will need to use array_reduce for the special format you have

$investamount = DB::table('investment')->pluck('amount')->toArray();
$investamount = array_reduce($investamount, function($carry, $item) {
    return $carry + str_replace(',','',$item);
});
N69S
  • 16,110
  • 3
  • 22
  • 36
0

use

$investamount = DB::table('investment')->sum('amount');

or

$investamount = DB::table('investment')->pluck('amount')->toArray();
$sum = array_sum($investamount);

EDITED

$investamount = DB::table('investment')->select(DB::raw("SUM(amount) as sum"))->get();
return $investamount->sum;
Hamelraj
  • 4,676
  • 4
  • 19
  • 42
0

Here is what you need

return DB::table('investment')->get()->sum(function ($investment) {
    return (float) str_replace(',', '', $investment->amount);
});
AH.Pooladvand
  • 1,944
  • 2
  • 12
  • 26
0

You can achieve that using laravel Collections. Then map the items to replace the commas.

Example:

return collect(DB::table('investment')->select('amount')->get())->map(function ($item) {
  return str_replace(',', '', $item->amount);
})->sum(); //This will return 425406‬.00

If you want to display the sum with commas in your blade template you can use number_format()

Example:

{{ number_format(sum, 2) }} //425,406‬.00
Zoren Konte
  • 306
  • 4
  • 12