1

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');
2
  • Does this answer your question? Laravel Eloquent Sum of relation's column Commented Mar 6, 2020 at 8:13
  • no it does not answer my question Commented Mar 6, 2020 at 8:19

5 Answers 5

2

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);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Undefined variable: carry
compact(): Undefined variable: investamount
@PPK try it again, was a mistype. investAmount with a capital A
@PPK it was in the variable $sum, i've edited it. Here, naming is as you wanted..
Worked this is the answer
|
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()

11 Comments

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SUM(amount)' in 'field list
@PPK you can use the sum method directly
You don't need to use get() when using the aggregate method as sum. Use
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??
are 703 stored on separate columns?
|
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;

3 Comments

i need the answer of 212,703.00 + 212,703.00, but what i am getting is 424 as answer.
i think you save amount as sting , so Answer has updated plz try
0

Here is what you need

return DB::table('investment')->get()->sum(function ($investment) {
    return (float) str_replace(',', '', $investment->amount);
});

2 Comments

Cannot use object of type stdClass as array
wont work, the "," is the thousands separator not the decimal one. there is already a "." in the number
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.