laravel ownsTo 给出 null

laravel belongsTo gives null(laravel ownsTo 给出 null)
本文介绍了laravel ownsTo 给出 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

laravel 中的 belongsTo 关系返回 null

belongsTo relation in laravel is returning null

public function games(){
    return $this->belongsTo('AppModelsGame');
  }

但是当我提供密钥作为参数时,它似乎工作正常

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

  public function games(){
        return $this->belongsTo('AppModelsGame','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 ownsTo 给出 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)