Laravel所属返回null [英] Laravel belongsTo return null

查看:118
本文介绍了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关系返回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();

我已尝试使用addselectwith 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆