2017-05-25 72 views
0

我有兩個模型Laravel屬於關聯返回null

字型號:

class Word extends Model 
{ 
public function pos() 
    { 
     return $this->belongsTo(Pos::class); 
    } 
} 

的PoS型號

class Pos extends Model 
{ 
    // 
    protected $table = 'pos'; 
    public $timestamps = false; 
} 

現在控制器我想wordpos關係,但pos關係返回n ULL當我指定列

控制器

$word = Word::with(['pos'])->whereId($vocabulary->word_id)->get(['id', 'word', 'surface', 'gloss', 'example','sound_url'])->first(); 

注意到當我使用查詢,而不指定列完美地工作

$word = Word::with(['pos'])->whereId($vocabulary->word_id)->first(); 

我一直在嘗試使用addselectwith closure但同結果

+0

確保您也越來越這種關係是基於列。 – aynber

+0

是否'字:: whereId($ vocabulary-> word_id) - >獲取([ '身份證', '文字', '面', '光澤', '例如', 'sound_url']) - >第一(); 「工作? – apokryfos

+0

@apokryfos是的,當然 –

回答

0

也許嘗試將pos_id添加到該列數組。

+0

請閱讀我的問題..它的工作沒有指定列 –

+0

是的,我理解你的問題......當你指定列時,你不指定專欄負責關係,所以也許這就是爲什麼它不工作。因此,將'pos_id'添加到['id','word','surface','gloss','example','sound_url']這個數組。 – sebbz

+0

是的,你的答案是完美的 –