palla451
2 weeks ago

Sum of collection after a groupBy

Posted 2 weeks ago by palla451

hi guy, i have this query:


$inputMounth = Input::whereDate('date','>=', Carbon::now()->subDays('30'))
                                ->orderBy('date','asc')->get()
                                     ->groupBy('date');

and this is the result


{
2020-03-16: [
{
id: 2,
user_id: 1,
description: "Enel",
import: "678",
date: "2020-03-16",
created_at: "2020-03-24T10:53:06.000000Z",
updated_at: "2020-03-24T10:53:06.000000Z"
}
],
2020-03-23: [
{
id: 3,
user_id: 1,
description: "pippo",
import: "16,78",
date: "2020-03-23",
created_at: "2020-03-24T13:51:27.000000Z",
updated_at: "2020-03-24T13:51:27.000000Z"
}
],
2020-03-24: [
{
id: 1,
user_id: 1,
description: "Gas",
import: "169,78",
date: "2020-03-24",
created_at: "2020-03-24T10:50:57.000000Z",
updated_at: "2020-03-24T10:50:57.000000Z"
},
{
id: 4,
user_id: 1,
description: "Ciccio",
import: "198,78",
date: "2020-03-24",
created_at: "2020-03-24T14:24:58.000000Z",
updated_at: "2020-03-24T14:24:58.000000Z"
},
{
id: 5,
user_id: 1,
description: "test",
import: "90",
date: "2020-03-24",
created_at: "2020-03-24T15:11:47.000000Z",
updated_at: "2020-03-24T15:11:47.000000Z"
}
]
}

I would like to get the total import for everyday. I tried this, but it doesn't work



$inputRes = $inputMounth->map(function ($result, $key) {
                        return [$key => $result->sum('import')];
        });

the "import" is a string so it would go to a float

how can I do, please?

Please sign in or create an account to participate in this conversation.