簡體   English   中英

Laravel雄辯地說出了一對多的關系

[英]Laravel eloquent one to many relation where statement

我在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表的Ids,主題表中我有(id,name)列,以及interviewList表(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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM