简体   繁体   English

Laravel所属返回null

[英]Laravel belongsTo return null

i have two model 我有两种模式

Word Model: 字模型:

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

and PoS Model : PoS模型

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

now in controller i want to get word and pos relationship but pos relationship return null when i specify the columns 现在在控制器中我想获取wordpos关系,但是当我指定列时pos关系返回null

Controller 调节器

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

note when i use query without specify the columns work perfectly 请注意当我使用查询而不指定列完美地工作

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

i have been tried use addselect , with closure but same result 我已经尝试过使用addselectwith closure但相同的结果

也许尝试将pos_id添加到该columns数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM