Laravel - 如何从另一个关系中获取数据 table
Laravel - How to get data from another relation table
我有 4 个 table 这样的:
quizzes
id | title
questions
id | question
question_quiz
id | quiz_id | question_id
question_options
id | question_id | option | is_answer
测验模型
/**
* Get the questions for the quiz.
*/
public function questions()
{
return $this->belongsToMany('App\Question');
}
问题模型
/**
* Get the question options for the question.
*/
public function question_options()
{
return $this->hasMany('App\QuestionOption');
}
测验 table 如何从 question_options table 中检索数据?
如果您的测验模型名称是测验,那么这可能对您有所帮助。这将为您提供属于测验的问题的所有相关选项。
Quiz::with('questions.question_options')->get();
Quiz::with('questions.question_options')->where('status', 'ACTIVE')->get();
使用这个你可以得到问题和那个问题的question_options
响应将是
[
{
id: 1,
title: quiz,
questions: [{
id: 1,
question: question,
question_options: [
{
id: 1,
question_id: question,
option: option 1
is_answer: 1
},
{
id: 2,
question_id: question,
option: option 2
is_answer: 0
},
{
id: 3,
question_id: question,
option: option 3
is_answer: 0
},
]
}]
}
]
我有 4 个 table 这样的:
quizzes
id | title
questions
id | question
question_quiz
id | quiz_id | question_id
question_options
id | question_id | option | is_answer
测验模型
/**
* Get the questions for the quiz.
*/
public function questions()
{
return $this->belongsToMany('App\Question');
}
问题模型
/**
* Get the question options for the question.
*/
public function question_options()
{
return $this->hasMany('App\QuestionOption');
}
测验 table 如何从 question_options table 中检索数据?
如果您的测验模型名称是测验,那么这可能对您有所帮助。这将为您提供属于测验的问题的所有相关选项。
Quiz::with('questions.question_options')->get();
Quiz::with('questions.question_options')->where('status', 'ACTIVE')->get();
使用这个你可以得到问题和那个问题的question_options
响应将是
[
{
id: 1,
title: quiz,
questions: [{
id: 1,
question: question,
question_options: [
{
id: 1,
question_id: question,
option: option 1
is_answer: 1
},
{
id: 2,
question_id: question,
option: option 2
is_answer: 0
},
{
id: 3,
question_id: question,
option: option 3
is_answer: 0
},
]
}]
}
]