laravel如何看懂报错提示

举个例子

只提到了 是——form.blade.php中的错误,但是

<form class="form-horizontal" method="post" action="">

    {{ csrf_field() }}

    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">姓名</label>

        <div class="col-sm-5">
            <input type="text" name="Student[name]"
                   value="{{ old('Student')['name'] ? old('Student')['name'] : $student->name }}"
                   class="form-control" id="name" placeholder="请输入学生姓名">
        </div>
        <div class="col-sm-5">
            <p class="form-control-static text-danger">{{ $errors->first('Student.name') }}</p>
        </div>
    </div>
    <div class="form-group">
        <label for="age" class="col-sm-2 control-label">年龄</label>

        <div class="col-sm-5">
            <input type="text" name="Student[age]"
                   value="{{ old('Student')['age'] ?  old('Student')['age'] : $student->age }}"
                   class="form-control" id="age" placeholder="请输入学生年龄">
        </div>
        <div class="col-sm-5">
            <p class="form-control-static text-danger">{{ $errors->first('Student.age') }}</p>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">性别</label>

        <div class="col-sm-5">
            @foreach($student->sex() as $ind=>$val)
                <label class="radio-inline">
                    <input type="radio" name="Student[sex]"
                           {{ isset($student->sex) && $student->sex == $ind ? 'checked' : ''  }}
                           value="{{ $ind }}"> {{ $val }}
                </label>
            @endforeach
        </div>
        <div class="col-sm-5">
            <p class="form-control-static text-danger">{{ $errors->first('Student.sex') }}</p>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">提交</button>
        </div>
    </div>
</form>

改正后为: {{ isset($student->sex) && $student->sex == $ind ? ‘checked’ : ‘’ }}
才正常。那么大家是如何知道是这一行的问题的呢?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

file

我的翻译大概是这样: 关联方法必须返回一个对象类型
就是说你模型有sex()方法

pubic function sex()
{
    return 'hello world';  // 你返回的不是对象
    // return $this->hasOne(User::class); 这个返回对象
}

错误原因: $student->sex没有这个属性,就会调用sex() 做为关联方法!

我怎么知道的: 踩坑 + 慢慢理解框架本质

说白了就是 经验 + 技术

其他方式… 不知道

3年前 评论
fatrbaby 3年前
caoayu 3年前
Richard852555 (楼主) 3年前
讨论数量: 4

file

我的翻译大概是这样: 关联方法必须返回一个对象类型
就是说你模型有sex()方法

pubic function sex()
{
    return 'hello world';  // 你返回的不是对象
    // return $this->hasOne(User::class); 这个返回对象
}

错误原因: $student->sex没有这个属性,就会调用sex() 做为关联方法!

我怎么知道的: 踩坑 + 慢慢理解框架本质

说白了就是 经验 + 技术

其他方式… 不知道

3年前 评论
fatrbaby 3年前
caoayu 3年前
Richard852555 (楼主) 3年前

很明显提示了Relationship method must return an object of type,这里说明模型返回的值是对象,从模型返回的值都是以对象的形式返回,所有获取就要使用$student->sex这样获取

3年前 评论

file

我排错:

  1. 先看第1部分,有错误说明,根据说明看,能不能找到问题
  2. 如果1 不能定位问题文件, 需要第2 部分的 trace 来找到调用的位置(1报错可能是 throw new Exception() 的位置)
  3. 重要,也不重要,就是背英语单词,你要看懂 1 部分报错是什么意思,也可以复制百度搜下,大部分是有的(如果小众用户框架,可能会搜不到,需要懂点英文了)
3年前 评论
kis龍 (作者) 3年前
Richard852555 (楼主) 3年前

如果英文不好,对文档也不熟悉,建议在代码里面逐行dd()或者从内而外从下到上逐次删除代码在运行,可以知道是哪行代码的问题,我见过做了2年多的laravel还是不会定位问题在哪一行的人,感觉排查问题和自身悟性有关,可能天生的吧

3年前 评论
Richard852555 (楼主) 3年前
赌赌赌赌赌赌赌圣! (作者) 3年前
Richard852555 (楼主) 3年前
赌赌赌赌赌赌赌圣! (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!