Laravel所属返回null [英] Laravel belongsTo return null
本文介绍了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;
}
现在在控制器中,我想获取word
和pos
关系,但是当我指定列时pos
关系返回null
now in controller i want to get word
and pos
relationship but pos
relationship return null when i specify the columns
控制器
$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();
我已尝试使用addselect
,with closure
但结果相同
i have been tried use addselect
, with closure
but same result
推荐答案
也许尝试将pos_id
添加到该column数组.
Maybe try to add pos_id
to that columns array.
这篇关于Laravel所属返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文