laravel-admin 提交表单时遇到 “Call to a member function getAttribute() on null” 报错的解决

laravel-admin 提交表单时遇到 “Call to a member function getAttribute() on null” 报错的解决

Tony哥
2023-05-08 / 0 评论 / 416 阅读 / 正在检测是否收录...
  • 先看下报错内容:

    {
    "message": "Call to a member function getAttribute() on null",
    "exception": "Error",
    "file": "D:\\WWW\\src\\vendor\\encore\\laravel-admin\\src\\Form.php",
    "line": 413,
    "trace": [
      {
        "file": "D:\\WWW\\src\\vendor\\encore\\laravel-admin\\src\\Form.php",
        "line": 393,
        "function": "applayFieldDisplay",
        "class": "Encore\\Admin\\Form",
        "type": "->"
      },
      {
        "file": "D:\\WWW\\src\\vendor\\encore\\laravel-admin\\src\\Form.php",
        "line": 352,
        "function": "ajaxResponse",
        "class": "Encore\\Admin\\Form",
        "type": "->"
      },
    ...
    }
  • 这是一个快捷表单提交问题的报错问题,问题困扰了我好长时间,起初以为是我哪里我弄错类型了,反复尝试无果,最终顺着错误来源去撸了一遍,发现“src/vendor/encore/laravel-admin/src/Form.php”的 413行
    lhe7hczc.png

$newValue = $this->model->fresh()->getAttribute($field->column());的fresh返回null 所以导致 “getAttribute” 报错,好了我们寻找下 fresh 看是做啥用的
lhe7k6mp.png

  • Reload a fresh model instance from the database.
  • 这么一看没问题,这是从数据库加载实例用的,那我就无解了,只能怀疑是 “$this->exists” 了,继续撸,于是我找到了这个方法,马上顿悟!
    lhe7miym.png
报错是因为没有执行到 $this->exists = true; 导致实例为空,我看了下注释解决思路来了,原来表设计的时候我的id主键是非自增的,需要我在模型中明确为非自增

解决:找到我的model,新增一行 public $incrementing=false; 搞定

0

评论 (0)

取消