You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel9删除评论报错Attempt to read property "userID" on bool

报错原因

Attempt to read property "userID" on bool错误触发逻辑非常明确:你传入Blade视图的$doctor_reviews变量实际是布尔值false,当模板尝试遍历该变量、读取其下的userID属性时就会抛出异常。你的代码存在4处逻辑/语法错误,按以下方案修复即可正常运行。


具体问题与修复方案

  • 第一处:deletereview方法取值、返回视图逻辑全错
    你列表页的删除链接是将评论ID拼在URL路径中(格式为/deletereview/{ID}),但该方法里通过$request->doctorReviewID取参数,根本拿不到ID值,导致查询时无匹配条件,first()返回false;后续你又把这个false以doctor_reviews为名传给了需要遍历评论集合的列表页,直接触发属性读取错误。
    修复逻辑:该方法的作用是展示删除确认页,应该通过路由参数获取评论ID,查询到对应评论后传给删除确认视图,而非传回列表页。修复后代码:

    public function deletereview($doctorReviewID){
        // 查不到对应评论直接返回404,避免返回false
        $review = Doctor_review::findOrFail($doctorReviewID);
        // 传给删除确认页,删除页视图路径为admin.deletereview
        return view('admin.deletereview', compact('review'));
    }
    
  • 第二处:删除提交方法缺少空值兜底
    现有代码直接拿查询结果调用delete(),如果请求携带的评论ID不存在,first()返回false,调用实例方法会直接报错。改成用findOrFail做兜底,查不到自动返回404页面:

    public function deletereviewSubmit(Request $request){
        $review = Doctor_review::findOrFail($request->doctorReviewID);
        $review->delete();
        return redirect()->route('docreviews');
    }
    
  • 第三处:评论列表Blade缺少语法闭合,链接写法不规范
    现有列表页的@foreach没有写@endforeach闭合标签,删除链接硬编码路径容易出现匹配错误,同时缺少操作列表头。修复后列表页关键代码:

    @extends('layouts.appAdmin')
    @section('contentAdmin')
        <table class = "table table-border">
            <tr>
                <th>Patient ID</th>
                <th>Doctor ID</th>
                <th>Description</th>
                <th>Rating</th>
                <th>Operation</th>
            </tr>
            @foreach($doctor_reviews as $review)
            <tr>
                <td>{{$review->userID}}</td>
                <td>{{$review->doctorID}}</td>
                <td>{{$review->description}}</td>
                <td>{{$review->point}}</td>
                <td><a href="{{ route('deletereview', $review->doctorReviewID) }}">Delete</a></td>
            </tr>
            @endforeach
        </table>
    @endsection
    
  • 第四处:删除表单页存在语法错误、路由指向错误
    现有隐藏域输出{{review->doctorReviewID}}漏了$符号,表单action指向了删除页展示路由而非删除提交路由,展示字段未填充已查询到的评论数据。修复后删除表单关键代码:

    <head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    </head>
    <body>
        <div class="container" style="width:500px;">  
        <h2>Delete review</h2> 
            <form action="{{ route('deletereviewSubmit') }}" method="POST">  
                @csrf
                <input type="hidden" name="doctorReviewID" value="{{ $review->doctorReviewID }}">
                <div class="form-group">
                    <label for="userID">Patient ID</label>
                    <input type="text" class="form-control" id="userID" name="userID" value="{{ $review->userID }}" readonly>
                </div>
                <div class="form-group">
                    <label for="doctorID">Doctor ID</label>
                    <input type="text" class="form-control" id="doctorID" name="doctorID" value="{{ $review->doctorID }}" readonly>
                </div>
                <div class="form-group">
                    <label for="description">Description</label>
                    <input type="text" class="form-control" id="description" name="description" value="{{ $review->description }}" readonly>
                </div>
                <div class="form-group">
                    <label for="point">Rating</label>
                    <input type="text" class="form-control" id="point" name="point" value="{{ $review->point }}" readonly>
                </div>
                <div class="form-group p-1">
                    <span>
                        <input type="submit" name="Delete" value="Delete" class="btn btn-info">
                    </span>
                </div>
                </div>
            </form> 
        </div>
    </body>
    </html>
    

    展示类输入框添加readonly属性,避免删除确认阶段用户篡改内容。


路由配置参考

确保routes/web.php中路由和控制器方法一一对应:

// 评论列表路由
Route::get('/docreviews', [DoctorReviewController::class, 'index'])->name('docreviews');
// 删除确认页路由
Route::get('/deletereview/{doctorReviewID}', [DoctorReviewController::class, 'deletereview'])->name('deletereview');
// 删除提交路由
Route::post('/deletereview', [DoctorReviewController::class, 'deletereviewSubmit'])->name('deletereviewSubmit');

优化建议:模型类名建议遵循Laravel驼峰命名规范,使用DoctorReview替代Doctor_review,避免框架隐式绑定、自动匹配时出现不必要的问题。

内容的提问来源于stack exchange,提问作者MAHFUZA SHARMILI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 00:24:38