Laravel v.5.8:
I want to group each child relation budgets and json serialize it.
$monthbudgets = \App\BudgetMonth::where('month', $curmonth)->with("budgets")->get(); foreach ($monthbudgets as $month) { $month->budgets = $month->budgets->groupBy("customer_id"); } dd($monthbudgets[8]); which will return:
//laravel collection of "budgets" ...[ 1 => [ 0 => [] 1 => [] ], 5 => [ 0 => [] ] ]... but using dd($monthbudgets[8]->jsonSerialize()), it will return:
//array of "budgets" ...[ 0 => [] 1 => [] 2 => [] ]... Looks like it flattens it and removes the keys (1, 5).
I'm also open to suggestions for minimizing the grouping loop. I tried
->with(["budgets" => function ($query) { $query->groupBy("customer_id"); }])->get(); which will result in
Syntax error or access violation: 1055 'budgets.id' isn't in GROUP BY (...)