Skip to main content
etiquetas editadas; título editado
Enlace
user128299
user128299

¿ Convertir correctamente una sentencia mysql a eloquent?al query builder de Laravel

se añadieron 582 caracteres en el cuerpo
Origen Enlace
user128299
user128299
 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id ```
  

     lo que he intentado

Lo que he intentado

$ResultUser=ResultQuestionnaire::select('resultquestionnaires.user_id','users.people_id',
        'peoples.names','resultquestionnaires.repetition_id','resultquestionnaires.questionnaire_id'
        ,'resultquestionnaires.score','resultquestionnaires.question_id','questions.statement')

            ->join('users','users.id','=','resultquestionnaires.user_id')
            ->join('peoples','peoples.id','=','users.people_id')
            ->join('questions','questions.id','=','resultquestionnaires.question_id')
            ->whereNotNull('resultquestionnaires.score')
            ->where('resultquestionnaires.user_id',$userId)
            ->where('resultquestionnaires.repetition_id',$repetitionId)
            ->where('resultquestionnaires.questionnaire_id',$questionnaireId)
            ->get();
    
     ########  modelo ResultQuestionnaire

modelo ResultQuestionnaire

class ResultQuestionnaire extends Model
{
    protected $table = 'resultquestionnaires';
    protected $fillable =
        [  'score','questionnaire_id','user_id',
        'question_id','correctoption_id','repetition_id','pass'];

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function question(){
        return $this->belongsTo('App\Question','question_id');
    }
    public function correctoption(){
        return $this->belongsTo('App\Correctoption','correctoption_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function repetition(){
        return $this->belongsTo('App\Repetition','repetition_id');
    }

}

   ########    modelo Question
    

modelo Question

class Question extends Model
{
    protected $fillable = [
        'image', 'statement', 'option_a',
        'option_b', 'option_c','option_d',
        'correctoption_id','questionnaire_id',

    ];
    protected $table ='questions';

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');

    }
    public function correctoption(){
        return$this->belongsTo('App\CorrectOption', 'correctoption_id');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

   ########    modelo people
    

modelo people

class People extends Model
{
    protected $fillable = [
        'document', 'names','lastnames', 'datebirth',
        'phone', 'addres', 'jobtitle_id', 'area_id',
    ];
//para indicar a la tabla que pertenece
    protected $table = 'peoples';

    public function user(){
        return $this->hasOne('App\User');
    }
    public function area(){
        return $this->belongsTo('App\Area','area_id');
    }
    public function jobtitle(){
        return $this->belongsTo('App\Jobtitle','jobtitle_id');
    }

}

    ########   modelo CorrectOption

modelo CorrectOption

class CorrectOption extends Model
{
    //
    protected $table = 'correctoptions';
    public function question()
    {
        return $this->hasOne('App\Question');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

  ########  Modelo Repetition
    

Modelo Repetition

class Repetition extends Model
{
    protected $fillable = [
        'quantity','questionnaire_id','user_id',
    ];
    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function resultquestionnaire(){
        return $this->hasMany('App\ResultQuestionnaire');
    }

}

      ######## Modelo area y jobtitle

Modelo area y jobtitle

class Area extends Model
{
    protected $fillable = [
         'name','description',
    ];

    public function people(){
        return $this->hasOne('App\People','id_people','id');
    }
}
#########
    class Jobtitle extends Model
{
    protected $fillable = [
        'title','description',
    ];
    public function people(){
        return $this->hasOne('App\People','id_people', 'id');
    }
}
### modelo user 
 

modelo user

public function people(){
        return $this->belongsTo('App\People');
    }
    public function role(){
        return $this->belongsTo('App\Role');
    }
    public function questionnaires(){
        return $this->hasMany('App\Questionnaire');

    }
    public function resultquestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
    public function repetitions(){
        return $this->hasMany('App\Repetition');
    }

 
 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id ```
  

     lo que he intentado

$ResultUser=ResultQuestionnaire::select('resultquestionnaires.user_id','users.people_id',
        'peoples.names','resultquestionnaires.repetition_id','resultquestionnaires.questionnaire_id'
        ,'resultquestionnaires.score','resultquestionnaires.question_id','questions.statement')

            ->join('users','users.id','=','resultquestionnaires.user_id')
            ->join('peoples','peoples.id','=','users.people_id')
            ->join('questions','questions.id','=','resultquestionnaires.question_id')
            ->whereNotNull('resultquestionnaires.score')
            ->where('resultquestionnaires.user_id',$userId)
            ->where('resultquestionnaires.repetition_id',$repetitionId)
            ->where('resultquestionnaires.questionnaire_id',$questionnaireId)
            ->get();
    
     ########  modelo ResultQuestionnaire

class ResultQuestionnaire extends Model
{
    protected $table = 'resultquestionnaires';
    protected $fillable =
        [  'score','questionnaire_id','user_id',
        'question_id','correctoption_id','repetition_id','pass'];

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function question(){
        return $this->belongsTo('App\Question','question_id');
    }
    public function correctoption(){
        return $this->belongsTo('App\Correctoption','correctoption_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function repetition(){
        return $this->belongsTo('App\Repetition','repetition_id');
    }

}

   ########    modelo Question
    class Question extends Model
{
    protected $fillable = [
        'image', 'statement', 'option_a',
        'option_b', 'option_c','option_d',
        'correctoption_id','questionnaire_id',

    ];
    protected $table ='questions';

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');

    }
    public function correctoption(){
        return$this->belongsTo('App\CorrectOption', 'correctoption_id');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

   ########    modelo people
    class People extends Model
{
    protected $fillable = [
        'document', 'names','lastnames', 'datebirth',
        'phone', 'addres', 'jobtitle_id', 'area_id',
    ];
//para indicar a la tabla que pertenece
    protected $table = 'peoples';

    public function user(){
        return $this->hasOne('App\User');
    }
    public function area(){
        return $this->belongsTo('App\Area','area_id');
    }
    public function jobtitle(){
        return $this->belongsTo('App\Jobtitle','jobtitle_id');
    }

}

    ########   modelo CorrectOption

class CorrectOption extends Model
{
    //
    protected $table = 'correctoptions';
    public function question()
    {
        return $this->hasOne('App\Question');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

  ########  Modelo Repetition
    class Repetition extends Model
{
    protected $fillable = [
        'quantity','questionnaire_id','user_id',
    ];
    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function resultquestionnaire(){
        return $this->hasMany('App\ResultQuestionnaire');
    }

}

      ######## Modelo area y jobtitle
class Area extends Model
{
    protected $fillable = [
         'name','description',
    ];

    public function people(){
        return $this->hasOne('App\People','id_people','id');
    }
}
#########
    class Jobtitle extends Model
{
    protected $fillable = [
        'title','description',
    ];
    public function people(){
        return $this->hasOne('App\People','id_people', 'id');
    }
}
### modelo user 
 public function people(){
        return $this->belongsTo('App\People');
    }
    public function role(){
        return $this->belongsTo('App\Role');
    }
    public function questionnaires(){
        return $this->hasMany('App\Questionnaire');

    }
    public function resultquestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
    public function repetitions(){
        return $this->hasMany('App\Repetition');
    }

 
 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id 

Lo que he intentado

$ResultUser=ResultQuestionnaire::select('resultquestionnaires.user_id','users.people_id',
        'peoples.names','resultquestionnaires.repetition_id','resultquestionnaires.questionnaire_id'
        ,'resultquestionnaires.score','resultquestionnaires.question_id','questions.statement')

            ->join('users','users.id','=','resultquestionnaires.user_id')
            ->join('peoples','peoples.id','=','users.people_id')
            ->join('questions','questions.id','=','resultquestionnaires.question_id')
            ->whereNotNull('resultquestionnaires.score')
            ->where('resultquestionnaires.user_id',$userId)
            ->where('resultquestionnaires.repetition_id',$repetitionId)
            ->where('resultquestionnaires.questionnaire_id',$questionnaireId)
            ->get();

modelo ResultQuestionnaire

class ResultQuestionnaire extends Model
{
    protected $table = 'resultquestionnaires';
    protected $fillable =
        [  'score','questionnaire_id','user_id',
        'question_id','correctoption_id','repetition_id','pass'];

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function question(){
        return $this->belongsTo('App\Question','question_id');
    }
    public function correctoption(){
        return $this->belongsTo('App\Correctoption','correctoption_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function repetition(){
        return $this->belongsTo('App\Repetition','repetition_id');
    }

}

modelo Question

class Question extends Model
{
    protected $fillable = [
        'image', 'statement', 'option_a',
        'option_b', 'option_c','option_d',
        'correctoption_id','questionnaire_id',

    ];
    protected $table ='questions';

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');

    }
    public function correctoption(){
        return$this->belongsTo('App\CorrectOption', 'correctoption_id');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

modelo people

class People extends Model
{
    protected $fillable = [
        'document', 'names','lastnames', 'datebirth',
        'phone', 'addres', 'jobtitle_id', 'area_id',
    ];
//para indicar a la tabla que pertenece
    protected $table = 'peoples';

    public function user(){
        return $this->hasOne('App\User');
    }
    public function area(){
        return $this->belongsTo('App\Area','area_id');
    }
    public function jobtitle(){
        return $this->belongsTo('App\Jobtitle','jobtitle_id');
    }

}

modelo CorrectOption

class CorrectOption extends Model
{
    //
    protected $table = 'correctoptions';
    public function question()
    {
        return $this->hasOne('App\Question');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

Modelo Repetition

class Repetition extends Model
{
    protected $fillable = [
        'quantity','questionnaire_id','user_id',
    ];
    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function resultquestionnaire(){
        return $this->hasMany('App\ResultQuestionnaire');
    }

}

Modelo area y jobtitle

class Area extends Model
{
    protected $fillable = [
         'name','description',
    ];

    public function people(){
        return $this->hasOne('App\People','id_people','id');
    }
}

class Jobtitle extends Model
{
    protected $fillable = [
        'title','description',
    ];
    public function people(){
        return $this->hasOne('App\People','id_people', 'id');
    }
}

modelo user

public function people(){
        return $this->belongsTo('App\People');
    }
    public function role(){
        return $this->belongsTo('App\Role');
    }
    public function questionnaires(){
        return $this->hasMany('App\Questionnaire');

    }
    public function resultquestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
    public function repetitions(){
        return $this->hasMany('App\Repetition');
    }
se añadieron 4681 caracteres en el cuerpo
Origen Enlace

Llevo un poco mas de 4 meses de usar laravel, pero últimamente me he sentido limitado por eloquent con las consultas sql que suelo hacer, he visto la documentación de laravel pero no termino de comprender, hay algo llamado DB:: loraw lo cual me permite realizar mi consulta sql sin eloquent, pero he investigado y usarlo de esta forma no es recomendable, también he usado los query builder, bueno sin mas, mi duda es esta, tengo el siguiente query(normalmente siempre los hago así para no perder la costumbre de la practica tradicional y luego construyó mi query builder):

 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id ```
  

     lo que he intentado

$ResultUser=ResultQuestionnaire::select('resultquestionnaires.user_id','users.people_id',
        'peoples.names','resultquestionnaires.repetition_id','resultquestionnaires.questionnaire_id'
        ,'resultquestionnaires.score','resultquestionnaires.question_id','questions.statement')

            ->join('users','users.id','=','resultquestionnaires.user_id')
            ->join('peoples','peoples.id','=','users.people_id')
            ->join('questions','questions.id','=','resultquestionnaires.question_id')
            ->whereNotNull('resultquestionnaires.score')
            ->where('resultquestionnaires.user_id',$userId)
            ->where('resultquestionnaires.repetition_id',$repetitionId)
            ->where('resultquestionnaires.questionnaire_id',$questionnaireId)
            ->get();
    
     ########  modelo ResultQuestionnaire

class ResultQuestionnaire extends Model
{
    protected $table = 'resultquestionnaires';
    protected $fillable =
        [  'score','questionnaire_id','user_id',
        'question_id','correctoption_id','repetition_id','pass'];

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function question(){
        return $this->belongsTo('App\Question','question_id');
    }
    public function correctoption(){
        return $this->belongsTo('App\Correctoption','correctoption_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function repetition(){
        return $this->belongsTo('App\Repetition','repetition_id');
    }

}

   ########    modelo Question
    class Question extends Model
{
    protected $fillable = [
        'image', 'statement', 'option_a',
        'option_b', 'option_c','option_d',
        'correctoption_id','questionnaire_id',

    ];
    protected $table ='questions';

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');

    }
    public function correctoption(){
        return$this->belongsTo('App\CorrectOption', 'correctoption_id');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

   ########    modelo people
    class People extends Model
{
    protected $fillable = [
        'document', 'names','lastnames', 'datebirth',
        'phone', 'addres', 'jobtitle_id', 'area_id',
    ];
//para indicar a la tabla que pertenece
    protected $table = 'peoples';

    public function user(){
        return $this->hasOne('App\User');
    }
    public function area(){
        return $this->belongsTo('App\Area','area_id');
    }
    public function jobtitle(){
        return $this->belongsTo('App\Jobtitle','jobtitle_id');
    }

}

    ########   modelo CorrectOption

class CorrectOption extends Model
{
    //
    protected $table = 'correctoptions';
    public function question()
    {
        return $this->hasOne('App\Question');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

  ########  Modelo Repetition
    class Repetition extends Model
{
    protected $fillable = [
        'quantity','questionnaire_id','user_id',
    ];
    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function resultquestionnaire(){
        return $this->hasMany('App\ResultQuestionnaire');
    }

}

      ######## Modelo area y jobtitle
class Area extends Model
{
    protected $fillable = [
         'name','description',
    ];

    public function people(){
        return $this->hasOne('App\People','id_people','id');
    }
}
#########
    class Jobtitle extends Model
{
    protected $fillable = [
        'title','description',
    ];
    public function people(){
        return $this->hasOne('App\People','id_people', 'id');
    }
}
### modelo user 
 public function people(){
        return $this->belongsTo('App\People');
    }
    public function role(){
        return $this->belongsTo('App\Role');
    }
    public function questionnaires(){
        return $this->hasMany('App\Questionnaire');

    }
    public function resultquestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
    public function repetitions(){
        return $this->hasMany('App\Repetition');
    }

Llevo un poco mas de 4 meses de usar laravel, pero últimamente me he sentido limitado por eloquent con las consultas sql que suelo hacer, he visto la documentación de laravel pero no termino de comprender, hay algo llamado DB:: lo cual me permite realizar mi consulta sql sin eloquent, pero he investigado y usarlo de esta forma no es recomendable, también he usado los query builder, bueno sin mas, mi duda es esta, tengo el siguiente query(normalmente siempre los hago así para no perder la costumbre de la practica tradicional y luego construyó mi query builder):

 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id ```
  

Llevo un poco mas de 4 meses de usar laravel, pero últimamente me he sentido limitado por eloquent con las consultas sql que suelo hacer, he visto la documentación de laravel pero no termino de comprender, hay algo llamado DB::raw lo cual me permite realizar mi consulta sql sin eloquent, pero he investigado y usarlo de esta forma no es recomendable, también he usado los query builder, bueno sin mas, mi duda es esta, tengo el siguiente query(normalmente siempre los hago así para no perder la costumbre de la practica tradicional y luego construyó mi query builder):

 select rq.id AS id,qnn.name  AS Cuestionario,
 qs.statement AS Preguntas,qs.option_a AS
 Opción_A,qs.option_b AS Opción_B,qs.option_c AS Opción_C,
 qs.option_d AS Opción_D,(case rq.correctoption_id 
 when '1' 
 then 'A' 
 when '2' 
 then 'B' 
 when '3' 
 then 'C'
 when '4' 
 then 'D' 
 end) 
 AS RespuestaSeleccionada
 ,co.option AS RespuestaCorrecta,
 (case rq.correctoption_id
 when qs.correctoption_id 
 then 'correcto' 
 else 'Mal' 
 end) AS Correctas
 ,p.names AS Nombres
 ,p.lastnames
 AS Apellidos,p.document AS Documento,p.phone AS Cel,usr.email AS Correo
 from resultquestionnaires rq 
 join questionnaires qnn 
 on qnn.id = rq.questionnaire_id 
 join questions qs on qs.id = rq.question_id
 join users usr on usr.id = rq.user_id 
 join correctoptions co on co.id = qs.correctoption_id  
 join peoples p on p.id = usr.people_id  
 order by rq.id ```
  

     lo que he intentado

$ResultUser=ResultQuestionnaire::select('resultquestionnaires.user_id','users.people_id',
        'peoples.names','resultquestionnaires.repetition_id','resultquestionnaires.questionnaire_id'
        ,'resultquestionnaires.score','resultquestionnaires.question_id','questions.statement')

            ->join('users','users.id','=','resultquestionnaires.user_id')
            ->join('peoples','peoples.id','=','users.people_id')
            ->join('questions','questions.id','=','resultquestionnaires.question_id')
            ->whereNotNull('resultquestionnaires.score')
            ->where('resultquestionnaires.user_id',$userId)
            ->where('resultquestionnaires.repetition_id',$repetitionId)
            ->where('resultquestionnaires.questionnaire_id',$questionnaireId)
            ->get();
    
     ########  modelo ResultQuestionnaire

class ResultQuestionnaire extends Model
{
    protected $table = 'resultquestionnaires';
    protected $fillable =
        [  'score','questionnaire_id','user_id',
        'question_id','correctoption_id','repetition_id','pass'];

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function question(){
        return $this->belongsTo('App\Question','question_id');
    }
    public function correctoption(){
        return $this->belongsTo('App\Correctoption','correctoption_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function repetition(){
        return $this->belongsTo('App\Repetition','repetition_id');
    }

}

   ########    modelo Question
    class Question extends Model
{
    protected $fillable = [
        'image', 'statement', 'option_a',
        'option_b', 'option_c','option_d',
        'correctoption_id','questionnaire_id',

    ];
    protected $table ='questions';

    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');

    }
    public function correctoption(){
        return$this->belongsTo('App\CorrectOption', 'correctoption_id');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

   ########    modelo people
    class People extends Model
{
    protected $fillable = [
        'document', 'names','lastnames', 'datebirth',
        'phone', 'addres', 'jobtitle_id', 'area_id',
    ];
//para indicar a la tabla que pertenece
    protected $table = 'peoples';

    public function user(){
        return $this->hasOne('App\User');
    }
    public function area(){
        return $this->belongsTo('App\Area','area_id');
    }
    public function jobtitle(){
        return $this->belongsTo('App\Jobtitle','jobtitle_id');
    }

}

    ########   modelo CorrectOption

class CorrectOption extends Model
{
    //
    protected $table = 'correctoptions';
    public function question()
    {
        return $this->hasOne('App\Question');
    }
    public function resultQuestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
}

  ########  Modelo Repetition
    class Repetition extends Model
{
    protected $fillable = [
        'quantity','questionnaire_id','user_id',
    ];
    public function questionnaire(){
        return $this->belongsTo('App\Questionnaire','questionnaire_id');
    }
    public function user(){
        return $this->belongsTo('App\user','user_id');
    }
    public function resultquestionnaire(){
        return $this->hasMany('App\ResultQuestionnaire');
    }

}

      ######## Modelo area y jobtitle
class Area extends Model
{
    protected $fillable = [
         'name','description',
    ];

    public function people(){
        return $this->hasOne('App\People','id_people','id');
    }
}
#########
    class Jobtitle extends Model
{
    protected $fillable = [
        'title','description',
    ];
    public function people(){
        return $this->hasOne('App\People','id_people', 'id');
    }
}
### modelo user 
 public function people(){
        return $this->belongsTo('App\People');
    }
    public function role(){
        return $this->belongsTo('App\Role');
    }
    public function questionnaires(){
        return $this->hasMany('App\Questionnaire');

    }
    public function resultquestionnaire()
    {
        return$this->hasMany('App\ResultQuestionnaire');
    }
    public function repetitions(){
        return $this->hasMany('App\Repetition');
    }

Origen Enlace
Loading