Good morning friends, I have this question of how to add these elements taken from several queries The result that shows me is this:
[{"s1":15},{"s2":12},{"s3":5},{"s4":7}]
What I need is to add them and then pass them to a view.
This is my driver
//Se consulta cada respuesta con funcion al ID del usuario
$s1 = DB::table('preguntas as p')
->join('respuestas as r', function($on){
$on->on('p.id_preguntas','=','r.id_preguntas');
})
->join('usuario as u', function($join){
$join->on('p.id_usu', '=', 'u.id_usu');
})
->select(DB::raw('SUM(respuesta1) as s1'))
->where('u.id_usu','=',"1")
->whereIn('p.preguntas',['A','G','M'])
->first();
//respuesta 2
$s2 = DB::table('preguntas as p')
->join('respuestas as r', function($on){
$on->on('p.id_preguntas','=','r.id_preguntas');
})
->join('usuario as u', function($join){
$join->on('p.id_usu', '=', 'u.id_usu');
})
->where('u.id_usu','=',"1")
->whereIn('p.preguntas',['B','H','N'])
// ->where('p.preguntas','=','A')
->select(DB::raw('SUM(respuesta2) as s2'))
->first();
//repuesta3
$s3 = DB::table('preguntas as p')
->join('respuestas as r', function($on){
$on->on('p.id_preguntas','=','r.id_preguntas');
})
->join('usuario as u', function($join){
$join->on('p.id_usu', '=', 'u.id_usu');
})
->where('u.id_usu','=',"1")
->whereIn('p.preguntas',['C','I','O'])
// ->where('p.preguntas','=','A')
->select(DB::raw('SUM(respuesta5) as s3'))
->first();
//respuesta4
$s4 = DB::table('preguntas as p')
->join('respuestas as r', function($on){
$on->on('p.id_preguntas','=','r.id_preguntas');
})
->join('usuario as u', function($join){
$join->on('p.id_usu', '=', 'u.id_usu');
})
->where('u.id_usu','=',"1")
->whereIn('p.preguntas',['D','J','P'])
// ->where('p.preguntas','=','A')
->select(DB::raw('SUM(respuesta4) as s4'))
->first();
$arr = array($s1,$s2,$s3,$s4);
// list($a[0],$a[1],$a[2],$a[3]) = $s;
return $arr;
// return $s1;
Try several ways with arrays but I have not been able to realize this sum, not if for this type of array definition it is different
I have seen that many in the arrays have it different from this one.
thanks for the help
EDIT ---
To what I refer, I am basically doing a survey system (small) but what I want is that these values that I throw are those that the user has already entered, and this same need to add, that is 15+12+5+7
and keep it in X variable, that's why in $arr = array($s1,$s2,$s3,$s4);
is what gave results of all the queries already placed above and as a result this gives me [{"s1":15},{"s2":12},{"s3":5},{"s4":7}]
I need help in this part to add these results (eye that are not static results)