You doubt about queries in MySql and pass data in laravel to a view

0

I have a question about the query tables in Mysql, I want to get different data about a survey and then add this data.

I understand that I have to call my data on the 3 tables so

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

I get to understand a bit the syntax of this but in Laravel I do not know how to apply it in the controller part.

and basically it is to obtain data from a survey, the values that I get are the same numbers when I get them, I add them and the total will be stored in a variable, but now I am more concerned with how to obtain the data. These are my tables.

My Users table

My table Questions

My Answers table

Basically to the complication that nothing else does not give me the logic is how I'm going to catch these results and show them by user, I hope you can help me get some orientation on this, I use Laravel 5.5 < strong> PHP 7.0 Mysql

I add the relations as I have them in my laravel project

User:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
//use Illuminate\Database\Eloquent\SoftDeletes;

class Usuario extends Model
{
    //use SoftDeletes;

    protected $table = 'usuario';

    protected $primaryKey = 'id_usu';

    protected $fillable = ['nombre','email'];

    // protected $dates = ['deleted_at'];

     public function usuario()
     {
        return $this->hasMany('App\Encuesta');
     }
}

Answers:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Respuestas extends Model
{
    protected $table = 'respuestas';

    protected $primaryKey = 'id_respuestas';    

    protected $filleable = [

        'respuesta1',
        'respuesta2',
        'respuesta3',
        'respuesta4',
        'respuesta5',
        'id_preguntas'
    ];

    public function encuesta()
    {

    return $this->belongsTo('App\Encuesta');

    }
}

Questions:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Encuesta extends Model
{
    protected $table = "preguntas";

    protected $primaryKey = 'id_preguntas';

    protected $fillable = [
        'preguntas',
        'id_usu',

    ];
    public function usuarios()
    {

    return $this->belonsTo('App\Usuario');

    }

    public function respuestas()
    {
        return $this->hasMany('App\Respuestas');
    }
}

I echo the query with Laravel as Oswuell post it and in effect he gives me this data

Code:

$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('p.id_preguntas','p.preguntas','r.id_respuestas','r.respuesta1','r.respuesta2','r.respuesta3','r.respuesta4','r.respuesta5')
        ->get();

        return $s1;

Result:

[{"id_preguntas":1,"preguntas":"A","id_respuestas":1,"respuesta1":"5","respuesta2":"4","respuesta3":"1","respuesta4":"2","respuesta5":"3"},{"id_preguntas":2,"preguntas":"B","id_respuestas":2,"respuesta1":"3","respuesta2":"2","respuesta3":"5","respuesta4":"1","respuesta5":"4"},{"id_preguntas":3,"preguntas":"C","id_respuestas":3,"respuesta1":"1","respuesta2":"2","respuesta3":"3","respuesta4":"4","respuesta5":"5"},{"id_preguntas":4,"preguntas":"D","id_respuestas":4,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":5,"preguntas":"E","id_respuestas":5,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":6,"preguntas":"F","id_respuestas":6,"respuesta1":"1","respuesta2":"2","respuesta3":"3","respuesta4":"4","respuesta5":"5"},{"id_preguntas":7,"preguntas":"G","id_respuestas":7,"respuesta1":"4","respuesta2":"5","respuesta3":"3","respuesta4":"1","respuesta5":"2"},{"id_preguntas":8,"preguntas":"H","id_respuestas":8,"respuesta1":"2","respuesta2":"3","respuesta3":"1","respuesta4":"4","respuesta5":"5"},{"id_preguntas":9,"preguntas":"I","id_respuestas":9,"respuesta1":"3","respuesta2":"5","respuesta3":"4","respuesta4":"2","respuesta5":"1"},{"id_preguntas":10,"preguntas":"J","id_respuestas":10,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":11,"preguntas":"K","id_respuestas":11,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":12,"preguntas":"L","id_respuestas":12,"respuesta1":"2","respuesta2":"3","respuesta3":"1","respuesta4":"5","respuesta5":"4"},{"id_preguntas":13,"preguntas":"M","id_respuestas":13,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":14,"preguntas":"N","id_respuestas":14,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":15,"preguntas":"O","id_respuestas":15,"respuesta1":"2","respuesta2":"1","respuesta3":"3","respuesta4":"4","respuesta5":"5"},{"id_preguntas":16,"preguntas":"P","id_respuestas":16,"respuesta1":"5","respuesta2":"2","respuesta3":"1","respuesta4":"4","respuesta5":"3"},{"id_preguntas":17,"preguntas":"Q","id_respuestas":17,"respuesta1":"5","respuesta2":"4","respuesta3":"3","respuesta4":"2","respuesta5":"1"},{"id_preguntas":18,"preguntas":"R","id_respuestas":18,"respuesta1":"5","respuesta2":"2","respuesta3":"3","respuesta4":"1","respuesta5":"4"},{"id_preguntas":19,"preguntas":"R","id_respuestas":19,"respuesta1":"5","respuesta2":"2","respuesta3":"3","respuesta4":"1","respuesta5":"4"}]

What would be this:

Now the question is to filter one more the results and add what the user answered (id_usu)

Edit:

$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'])
        ->get();
        //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',['A','G','M'])
       // ->where('p.preguntas','=','A')
        ->select(DB::raw('SUM(respuesta2) as s2')) 
        ->get();    

$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')) 
    ->get();   


    return $s1.$s2.$s3;

giving result:

[{"s1":15}][{"s2":12}][{"s3":5}]
    
asked by Dohko19 30.08.2018 в 20:50
source

2 answers

1

Try this

First consultation brings you all the questions

SELECT p.id_preguntas, p.preguntas, r.id_respuestas, r.respuesta1, r.respuesta2, r.respuesta3, r.respuesta4, r.respuesta5, u.id_usu, u.nombre, u.email FROM preguntas AS p INNER JOIN respuestas AS r ON p.id_preguntas = r.id_preguntas INNER JOIN usuarios AS u ON p.id_usu = u.id_usu

Second consultation brings you all the questions per user

SELECT p.id_preguntas, p.preguntas, r.id_respuestas, r.respuesta1, r.respuesta2, r.respuesta3, r.respuesta4, r.respuesta5, u.id_usu, u.nombre, u.email FROM preguntas AS p INNER JOIN respuestas AS r ON p.id_preguntas = r.id_preguntas INNER JOIN usuarios AS u ON p.id_usu = u.id_usu WHERE u.id_usu = '".$id."'
    
answered by 30.08.2018 / 21:21
source
2

I'm finding it hard to understand what exactly you need. I also do not understand why you define relationships in the Models or even so that you generate the models if you do not use them.
According to what I understand you need to add the results of the answers of each question of each user. If that is the case you could try something like:

1 - renombrá the name of the method in the User model. Instead of being called usuario() should be called encuestas() .

2- in the Model Encuesta I think that the relation respuestas() should be singular and a relation of hasOne since a question should have only one answer (this is what the logic tells me may not be the case, you will tell me if I'm wrong). It would look like this:

public function respuesta()
{
    return $this->hasOne('App\Respuestas');
}

3- Then we obtain the result of each question that if I understood correctly it consists of the summation of the 5 answers (answer1 + answer2 + answer3 + answer4 + answer5)

// Selecciono el usuario del que quiero obtener los resultados
$usuario = Usuario::find($id_usuario);

$resultados = collect();

// Por cada pregunta del usuario sumo las respuestas
foreach($usuario->encuestas as $encuesta) {
    $encuesta->resultado = array_sum(array_only($encuesta->respuesta, ['respuesta1', 'respuesta2', 'respuesta3', 'respuesta4', 'respuesta5']));
    $resultados->push($encuesta);
}

return view('VIEW_NAME')->with('encuestas_con_resultados', $resultados);

I hope I have understood well what you are needing and that this code (obviously without testing) will guide you a little on the way to the solution of your problem and also on how to use the relationships in Laravel.

    
answered by 31.08.2018 в 01:47