Mysql 回答和问题数据库的Laravel自定义查询
我有三张桌子,叫做考试、问题和答案。他们是通过外国id连接的。我有一个连接问答表的功能。我有一个问题和多个答案。当我加入时,我看到许多相同的问题,只有一个答案(这是正常的)。有没有办法通过查询生成器在一个查询(数组)中加入1个问题和多个答案 考试表Mysql 回答和问题数据库的Laravel自定义查询,mysql,laravel,laravel-5,query-builder,Mysql,Laravel,Laravel 5,Query Builder,我有三张桌子,叫做考试、问题和答案。他们是通过外国id连接的。我有一个连接问答表的功能。我有一个问题和多个答案。当我加入时,我看到许多相同的问题,只有一个答案(这是正常的)。有没有办法通过查询生成器在一个查询(数组)中加入1个问题和多个答案 考试表 $table->id(); $table->string('language_code'); $table->string('title'); $table->
$table->id();
$table->string('language_code');
$table->string('title');
$table->integer('subcategory_id')->nullable();
$table->string('section');
$table->string('class');
$table->string('subject')->nullable();
$table->string('time')->default('60');
$table->timestamps();
问题表
$table->id();
$table->integer('exam_id');
$table->string('q_pic')->nullable();
$table->string('q_name')->nullable();
$table->string('q_text');
$table->timestamps();
答案表
$table->id();
$table->integer('user_id');
$table->integer('question_id');
$table->text('user_answer')->nullable();
$table->integer('user_answer_id');
$table->integer('c_answer_id');
$table->timestamps();
这是我的视图函数
public function view($id)
{
$questions = DB::table('questions')
->leftJoin('answers','answers.question_id','=','questions.id')
->where('questions.exam_id','=',$id)
->get();
return view('main/view',compact('questions'));
}
如果正确设置了关系,则不需要连接即可实现此目的 你的关系应该是这样的:
//use App\Question;
$questionsWithAnswers = Question::where('id', $id)->with('answers')->get();
考试模式:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
问题模型:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
应答模式:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
现在您可以这样查询:
//use App\Question;
$questionsWithAnswers = Question::where('id', $id)->with('answers')->get();
这将为您提供一系列问题和相关答案。如果您正确设置了关系,则无需加入即可实现此目的 你的关系应该是这样的:
//use App\Question;
$questionsWithAnswers = Question::where('id', $id)->with('answers')->get();
考试模式:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
问题模型:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
应答模式:
public function questions()
{
return $this->hasMany(Question::class);
}
public function exam()
{
return $this->belongsTo(Exam::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
public function question()
{
return $this->belongsTo(Question::class);
}
现在您可以这样查询:
//use App\Question;
$questionsWithAnswers = Question::where('id', $id)->with('answers')->get();
这将为您提供一系列问题和相关答案