laravel includesTo为null [英] laravel belongsTo gives null

查看:77
本文介绍了laravel includesTo为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

belongsTo关系返回null

belongsTo relation in laravel is returning null

public function games(){
    return $this->belongsTo('App\Models\Game');
  }

但是当我提供key作为参数时,它似乎可以正常工作

but when i provide key as argument it seems to work fine

  public function games(){
        return $this->belongsTo('App\Models\Game','game_id');
      }

但根据 docs game_id本身是默认参数.我在这里缺少的小东西是什么.

but as per the docs game_id is itself is a default argument. What is the small thing i am missing here.

谢谢

推荐答案

关系的belongsTo端根据关系方法的名称构建外键名称.此行为不同于关系的hasOne/hasMany端,后者使用类的名称.

The belongsTo side of the relationship builds the foreign key name based on the name of the relationship method. This behavior is different than the hasOne/hasMany side of the relationship, which uses the name of the class.

在这种情况下,您的关系方法名为games,因此它将查找外键字段games_id.如图所示,您可以提供外键字段作为第二个参数,也可以将关系方法重命名为game().

In this case, your relationship method is named games, so it will look for the foreign key field games_id. You can either provide the foreign key field as the second parameter, as you have shown, or you can rename your relationship method to game().

这篇关于laravel includesTo为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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