拉拉维尔雄辩地一对多关系在哪里陈述


Laravel eloquent one to many relation where statement

我有以下模型在拉拉维尔雄辩

class InterviewList extends Eloquent{
protected  $table = 'interviewlists';
public function subject(){
    return $this->belongsTo('Subject','id_subject','id');
}}
class Subject extends Eloquent{
public  function interviewList(){
    return $this->hasMany('InterviewList');
}}

我需要获取主题名称为"java"的面试列表表的 ID,在主题表中,我有(id,name)列,在访谈列表表中有(id,name,id_subject)。我尝试了以下代码

 $interviewListId = InterviewList::with('Subject')->whereName('java')->get(array('id'));

但它只是没有给出结果

像这样的事情怎么样

Subject::where('name', 'like', '%java%')-> interviewList()->get(array('id'));

要获取具有名称java主题的面试列表的 ID,您应该使用:

$ids = InterviewList::whereHas('subject',  function($q)
{
    $q->where('name', 'java');
})->lists('id');