【发布时间】:2016-07-13 23:58:55
【问题描述】:
在索引控制器中,我不需要任何条件,因此可以使用所有关系模型非常清楚地检索数据。
$questions = Question::with('user','courses','subjects')->take(10)->orderBy("id","DESC")->get();
这将返回包含所有相关数据的问题列表,例如 user courses 和 subject
但是在课程控制器中,当尝试检索具有相同条件的数据并为课程 slug 添加条件时,它会返回错误。
$questions = Question::with('user','courses','subjects')->where("slug",$course)->take(10)->orderBy("id","DESC")->get();
因为它在问题表查询中添加了这个条件,所以没有slug 列。
当我使用course 类检索时,它返回正确的结果,但subjects 和users 丢失了
$questions = Course::with("question")->where("nick_name",$course)->orWhere("slug",$course)->orderBy("id","DESC")->take(10)->get();
那么我怎样才能得到所有相关的数据。 课程模型有
public function question(){
return $this->belongsToMany('App\Models\Question','university_questions')->take(10);
}
和问题模型有
public function courses(){
return $this->belongsToMany('App\Models\Course','course_questions');
}
public function subjects(){
return $this->belongsToMany('App\Models\Subject','subject_questions');
}
public function years(){
return $this->hasMany('App\Models\QuestionYear');
}
这里缺少什么请帮忙。
【问题讨论】: