douruduan8812 2016-06-22 14:53
浏览 191
已采纳

laravel belongsTo给出null

belongsTo relation in laravel is returning null

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

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

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

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

Thanks

  • 写回答

2条回答 默认 最新

  • douchenzhan3050 2016-06-22 15:14
    关注

    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.

    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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题