Laravel雄辩地说出了一对多的关系

时间:2015-03-15 12:39:28

标签: php mysql laravel laravel-4 eloquent

我在laravel雄辩中有以下模型

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"的interviewList表的ID 在主题表中我有(id,name)列,并在interviewList表(id,name,id_subject)中。 我尝试了以下代码

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

但它只是没有结果

2 个答案:

答案 0 :(得分:2)

这样的事情

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

答案 1 :(得分:1)

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

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

})->lists('id');