Redadublex
Redadublex

Reputation: 129

Laravel eloquent one to many relation where statement

I have the following models in laravel eloquent

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');
}}

I need to get Ids of interviewList table that subjects name is "java", in subject table i have (id,name) columns, and in interviewList table (id,name,id_subject). I'va tried the following code

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

but it just gives no result

Upvotes: 1

Views: 144

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

To get ids of Interview list that has subjects with name java you should use:

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

})->lists('id');

Upvotes: 1

Dhiraj
Dhiraj

Reputation: 33618

How about something like this

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

Upvotes: 2

Related Questions