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');
}