$loanAmountbyMonth = Loan::select('date_release','amount_granted')
->where('invalid',false)
->where('transaction_year', $transyear)
->get()
->groupBy(function($date) {
return Carbon::parse($date->date_release)->format('m'); // grouping by months
});
$loanmcount2 = [];
$loanArr2 = [];
foreach ($loanAmountbyMonth as $key => $value) {
$loanmcount2[(int)$key] = $value;
}
return view('src/modules-crud',compact('loanmcount2'));
Output:
string(471) "{"9":[{"date_release":"2021-09-21","amount_granted":"10000"}],"5":[{"date_release":"2021-05-23","amount_granted":"10000"},{"date_release":"2021-05-23","amount_granted":"20000"}],"6":[{"date_release":"2021-06-17","amount_granted":"10000"}],"7":[{"date_release":"2021-07-18","amount_granted":"15000"},{"date_release":"2021-07-18","amount_granted":"50000"},{"date_release":"2021-07-18","amount_granted":"14000"}],"8":[{"date_release":"2021-08-22","amount_granted":"10000"}]}"
My Desired Output:
string(61) "{"9":"10000","5":"30000","6":"10000","7":"79000","8":"10000"}"
I wanted to add the amount_granted under "7":[{"date_release":"2021-07-18","amount_granted":"15000"},{"date_release":"2021-07-18","amount_granted":"50000"},{"date_release":"2021-07-18","amount_granted":"14000"}] but I can only access the first array object value with amount granted equal to 15,000 whenever I used $value[0]['amount_granted]. If I use $value[1]['amount_granted'] it says undefined offset 1. How could I add together the amount granted in key 7? Thank you.