Я хочу взять элемент под названием vote_count
и подвести их итог, чтобы увидеть общее количество избирателей. Объект довольно большой, я удалю то, что не имеет отношения, и оставлю только важную часть.
{
"id":1,
"name":"Evaluacion al docente por parte de los estudiantes",
"description":"Evaluacion al docente de la clase de Electronica 1 de la carrera de Ingenieria",
"user_id":1,
"anonymous":0,
"created_at":"2019-12-09 10:30:50",
"updated_at":"2019-12-10 08:43:40",
"result":[
{
"id":1,
"type":1,
"rank":null,
"title":"Cual es su opinion acerca de la oficina de telematica",
"options":{
"1":{
"title":"answer question 1",
"vote_count":null
},
"7":{
"title":"answer question 1",
"vote_count":null
},
"13":{
"title":"answer question 1",
"vote_count":null
},
"19":{
"title":"answer question 1",
"vote_count":null
}
}
},
{
"id":2,
"type":2,
"rank":null,
"title":"elija 3 opciones",
"options":{
"1":{
"title":"opcion a",
"vote_count":1
},
"2":{
"title":"opcion b",
"vote_count":0
},
"3":{
"title":"opcion c",
"vote_count":1
},
"4":{
"title":"opcion d",
"vote_count":2
}
}
},
{
"id":5,
"type":1,
"rank":null,
"title":"texto aslkfjasd asd afsdf",
"options":{
"3":{
"title":"jdflaksfj alkdfj ajfawieur alk jaklf",
"vote_count":null
},
"9":{
"title":"sdf asd ads",
"vote_count":null
},
"15":{
"title":"sdfa sd fasdfads a",
"vote_count":null
},
"21":{
"title":"asdfasd",
"vote_count":null
}
}
},
{
"id":3,
"type":4,
"rank":null,
"title":"bota la basura en los basureros?",
"options":{
"1":{
"title":"No",
"vote_count":2
},
"2":{
"title":"Si",
"vote_count":1
}
}
},
{
"id":4,
"type":3,
"rank":6,
"title":"Que calificacion le daria a la limpieza general de la universidad?",
"options":{
"1":{
"title":1,
"vote_count":0
},
"2":{
"title":2,
"vote_count":1
},
"3":{
"title":3,
"vote_count":0
},
"4":{
"title":4,
"vote_count":1
},
"5":{
"title":5,
"vote_count":0
},
"6":{
"title":6,
"vote_count":1
}
}
},
{
"id":6,
"type":3,
"rank":3,
"title":"jaksjakjf fjf a jaskj fkf ja range",
"options":{
"1":{
"title":1,
"vote_count":1
},
"2":{
"title":2,
"vote_count":1
},
"3":{
"title":3,
"vote_count":1
}
}
}
],
//rest of it, that is not relevant
}
Как вы можете видеть, это вопросы и варианты, которые люди выбрали в том, что я пытаюсь считать в Чтобы позже показать% людей, которые выбрали вариант вопроса вместо # количество, так как некоторые из вопросов не являются обязательными, я должен рассчитать общее количество для каждого вопроса вместо всего опроса. Таким образом я сохраняю данные в контроллере и удаляю часть сохранения вопроса типа текста, потому что для этого типа вопроса не требуется общее количество
$survey = Survey::with('surveyQuestions.responseType',
'surveyQuestions.surveyQuestionOption', 'surveyQuestions.answer')
->where('id', $id)->first();
$result = [];
foreach ($survey['surveyQuestions'] as $question) {
$options = [];
//multiple choice
if ($question->response_type_id === 2) {
foreach ($question['surveyQuestionOption'] as $option) {
$count = 0;
foreach ($survey['answer'] as $answer) {
if ($answer->survey_question_id === $option->survey_question_id
&& (int)$answer->answer === $option->id) {
$count++;
}
$options[$option->id] = [
'title' => $option->option,
'vote_count' => $count
];
}
}
}
//ranking
if ($question->response_type_id === 3) {
if ($question->rank === 3){
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3]
];
} else if ($question->rank === 4){
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4]
];
} else if ($question->rank === 5){
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4],
['id' => 5, 'option' => 5]
];
} else {
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4],
['id' => 5, 'option' => 5],
['id' => 6, 'option' => 6]
];
}
foreach ($question['opciones'] as $opt) {
$count = 0;
foreach ($question['answer'] as $answer) {
if ($answer->survey_question_id === $question->id && (int)$answer->answer === $opt['option']) {
$count++;
}
$options[$opt['id']] = [
'title' => $opt['option'],
'vote_count' => $count
];
}
}
}
//yes or no
if ($question->response_type_id === 4) {
$question->opciones = [
['id' => 1, 'option' => 'No'],
['id' => 2, 'option' => 'Si']
];
foreach ($question['opciones'] as $opt) {
$count = 0;
foreach ($question['answer'] as $answer) {
if ($answer->survey_question_id === $question->id && $answer->answer === $opt['option']) {
$count++;
}
$options[$opt['id']] = [
'title' => $opt['option'],
'vote_count' => $count
];
}
}
}
$result[] = [
'id' => $question->id,
'type' => $question->response_type_id,
'rank' => $question->rank,
'title' => $question->question,
'options' => $options
];
}
Я действительно не уверен, где или как бы я посчитал сумму, любая помощь будет принята с благодарностью!