Home > Mobile >  Aggregate of a certain field for previous dates of the month in Laravel Eloquent?
Aggregate of a certain field for previous dates of the month in Laravel Eloquent?

Time:04-20

So, let's say I have a table with just id, date, and sales. I want a laravel eloquent query that shows the accumulated sales so far for that date for the month. For example:

What I want:

ID Date Sales Acc
1 2020-01-01 100 100
2 2020-01-02 50 150
3 2020-01-03 200 350
4 2020-02-01 220 220
5 2020-02-02 300 520

The above table is what I want from Eloquent, the actual table is without Acc.

I don't want to create a new column, I just want to show the accumulated through the eloquent query. Thank you!

CodePudding user response:

is not clear if you have a model already, but you mention you want to use eloquend. Let's assume you have a model called Sales. In your controller:

$sales = Sales::all();

$accTotal = $sales->map(function($sale){
   $add  = $sale->acc;  
});

return $add;

  • Related