[英]laravel one to many relationship column not found
I have the following models: 我有以下型号:
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');
}
}
In the controller I use: 在控制器中,我使用:
$questions = Question::with('answer')->whereHas(
'quiz', function($q) use($id) {$q->where('id', $id);
})->get();
I get the following error: 我收到以下错误:
Column not found: 1054 Unknown column 'answers.question_id' in 'where clause' (SQL: select * from
answers
whereanswers
.question_id
in (10, 11, 12, 13)))列未找到:1054未知列在'where子句' 'answers.question_id'(SQL:选择从*
answers
其中answers
。question_id
在(10,11,12,13)))
You should define your answer
relationship in your Question
model this way: 您应该通过以下方式在“
Question
模型中定义answer
关系:
public function answer(){
return $this->hasMany('Answer','id_question','id');
}
because as you said you have column id_question
and not question_id
what is default value in this case. 因为如您所说,您有
id_question
列而不是question_id
,在这种情况下,默认值为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.