Watch https://laracasts.com/series/code-katas-with-phpunit/episodes/9
How can I optimize this code and convert it to php code for the controller?
That code is not suitable for the controller.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to move the following logic to the controller because it makes my page extremely slow and I would like to optimize it if possible as well, I'm not the original author so I really can't say much about why the code is the way it is, but I'm sure it can be optimized. What the code is doing is: take survey questions and counting the answers and basically setting the data up to use with vue charts.js.
This is the code in js
let count = 1;
this.survey_question.map((item) => {
item.count = count;
count++;
return item;
});
this.survey_question.map((question) => {
if (question.response_type_id === 1) {
let count = 0;
let num = 0;
question.answer.map((answer_pager) => {
count++;
answer_pager.count = count;
});
question.answer.map((answer) => {
num++;
});
question.num = num;
}
if (question.response_type_id === 2) {
let count = 0;
let option = null;
this.data = [];
question.answer.map((answer) => {
let record = 0;
question.answer.map((answers) => {
this.data.push(answers.answer);
});
this.undata = this.data.filter((valor, indiceActual, arreglo) => arreglo.indexOf(valor) === indiceActual);
option = answer.survey_question.survey_question_option.map((options) => {
this.label.push(options.option);
this.data.forEach((evaluator) => {
if (options.id == evaluator) {
record++;
}
});
this.recording.push(record);
record = 0;
answer.count = count;
if (count === question.answer.length) {
answer = answer.data = {
labels: this.label,
datasets: [{
labels: 'Resultado',
backgroundColor: this.colors,
data: this.recording,
}]
}
}
count++;
});
this.recording = [];
this.data = [];
this.label = [];
});
}
if (question.response_type_id === 3) {
let count = 1;
let answers = null;
let uno = 0;
let dos = 0;
let tres = 0;
let cuatro = 0;
let cinco = 0;
let seis = 0;
question.answer.map((answer) => {
if (question.rank === 3) {
if (answer.answer === '1') {
uno++;
}
if (answer.answer === '2') {
dos++;
}
if (answer.answer === '3') {
tres++;
}
if (question.answer.length === count) {
answers = answer.data = {
labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas'],
datasets: [{
label: 'Resultado',
backgroundColor: this.colors,
data: [uno, dos, tres]
}]
}
}
}
if (question.rank === 4) {
if (answer.answer === '1') {
uno++;
}
if (answer.answer === '2') {
dos++;
}
if (answer.answer === '3') {
tres++;
}
if (answer.answer === '4') {
cuatro++;
}
if (question.answer.length === count) {
answers = answer.data = {
labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas'],
datasets: [{
label: 'Resultado',
backgroundColor: this.colors,
data: [uno, dos, tres, cuatro]
}]
}
}
}
if (question.rank === 5) {
if (answer.answer === '1') {
uno++;
}
if (answer.answer === '2') {
dos++;
}
if (answer.answer === '3') {
tres++;
}
if (answer.answer === '4') {
cuatro++;
}
if (answer.answer === '5') {
cinco++;
}
if (question.answer.length === count) {
answers = answer.data = {
labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas',
'Cinco Estrellas'
],
datasets: [{
label: 'Resultado',
backgroundColor: this.colors,
data: [uno, dos, tres, cuatro, cinco]
}]
}
}
}
if (question.rank === 6) {
if (answer.answer === '1') {
uno++;
}
if (answer.answer === '2') {
dos++;
}
if (answer.answer === '3') {
tres++;
}
if (answer.answer === '4') {
cuatro++;
}
if (answer.answer === '5') {
cinco++;
}
if (answer.answer === '6') {
seis++;
}
if (question.answer.length === count) {
answers = answer.data = {
labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas',
'Cinco Estrellas', 'Seis Estrellas'
],
datasets: [{
label: 'Resultado',
backgroundColor: this.colors,
data: [uno, dos, tres, cuatro, cinco, seis]
}]
}
}
}
question.data = answers;
count++;
});
}
if (question.response_type_id === 4) {
let answers = null;
let count = 1;
let countNo = 0;
let countYes = 0;
question.answer.map((answer) => {
if (answer.answer === 'No') {
countNo++;
} else {
countYes++;
}
if (question.answer.length === count) {
answers = answer.data = {
labels: ['Si', 'No'],
datasets: [{
label: 'Resultado',
backgroundColor: this.colors,
data: [countYes, countNo]
}]
};
}
question.data = answers;
count++;
});
}
});
There are 4 types of questions which are text (responese_type_id = 1), multiple choice (responese_type_id = 2), ranking (responese_type_id = 3) and yes or no (responese_type_id = 4).
How can I optimize this code and convert it to php code for the controller?
This is the solution to the code I posted in the comments
https://laracasts.com/discuss/channels/laravel/fix-foreach-loop-to-do-whats-excepted
Please or to participate in this conversation.