【发布时间】:2015-03-19 21:49:30
【问题描述】:
我正在我的控制器中编写一个函数来返回一个 json 响应。这是我的功能:
public function play($id){
$quiz = Quiz::find($id);
$questions = $quiz->question()->get();
return Response::json(array(
'quiz' => $quiz,
'questions' => $questions));
}
这很有效,但是,我也有与问题相关的答案。
在我的Question 模型中,我有这个answer() 函数:
public function answer(){
return $this->hasMany('Answer');
}
在我的Answer 模型中,我当然有我的question() 函数:
public function question(){
return $this->belongsTo('Question');
}
我如何获得答案?我不能使用;
$answers = $question->answer()->get;
我不能用
//get the questions using query, but this returns nothing
$questions = Question::where('quiz_id', '=', $id);
$answers = $questions->answer()->get;
我希望我足够清楚,我尝试搜索但我找不到任何东西,请任何人帮助:)?
【问题讨论】:
标签: php laravel orm model eloquent