我有以下型号:
class Question extends Eloquent
{
public function quiz()
{
return $this->belongsTo('Quiz','id_quiz','id');
}
public function answer()
{
return $this->hasMany('Answer');
}
}
class Answer extends Eloquent
{
public function question()
{
return $this->belongsTo('Question','id_question','id');
}
}
在我使用的控制器中:
$questions = Question::with('answer')->whereHas(
'quiz', function($q) use($id) {$q->where('id', $id);
})->get();
我收到以下错误:
找不到列:1054未知列'answer.question_id'在'where 子句''(SQL:select * from
answers
whereanswers
。question_id
in (10,11,12,13)))
答案 0 :(得分:6)
您应该以这种方式在answer
模型中定义Question
关系:
public function answer(){
return $this->hasMany('Answer','id_question','id');
}
因为正如您所说,您有列id_question
而不是question_id
在这种情况下默认值是什么。