2
尝试为Morris.js环形图创建一个JSON数组,但不能想出任何方法来获取正确的格式。有小费吗?Laravel创建一个JSON数组
我的控制器:
$user = User::find((Auth::user()->id));
$budget = $user->budget()->get();
$labels = ['rent', 'heating', 'utilities', 'internet_tv', 'phone', 'food', 'sweets', 'alcohol_cigs', 'insurance' , 'loans', 'finance_other', 'cosmetics'
, 'medicine', 'clothes_shoes', 'accessories', 'electronics', 'school', 'entertainment', 'food_out', 'holidays', 'books', 'pets', 'gifts', 'car', 'other'];
$data2 = [];
foreach ($labels as $label)
{
$data2['label'][] = $label;
$data2['value'][] = $budget->sum($label);
}
$data2 = json_encode($data2);
什么我得到:
'{"label":["rent","heating","utilities","internet_tv" ...],"value":[435,30,0,0 ...]}'
我想:
'[{"label":"rent","value":"435"},{"label":"heating","value":"30"},{"label":"utilities","value":"0"},{"label":"internet_tv","value":"0"} ...]'
它的工作!谢谢^^ –
谢谢!它节省了我的时间 – vipin