public function generateQuestionChart($question_id, $title) { $answers = Question::find($question_id)->answers; $content = $answers->lists('content'); $total = 0; $index = 0; $votes = array(); foreach ($answers as $answer) { $count = UserAnswer::where('answer_id', '=', $answer->id)->count(); $total += $count; $votes[$index++] = $count; } $index = 0; $percentages = array(); foreach ($votes as $vote) { $percentages[$index++] = round($vote / $total * 100, 0, PHP_ROUND_HALF_UP); } return array('title' => $title, 'xaxis' => $content, 'percentages' => $percentages, 'votes' => $votes, 'total' => $total); }