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

Laravel 5.8获取非对象属性'is_approved'错误排查与按钮控制

解决 "Trying to get property 'is_approved' of non-object" 错误

这个错误本质是你尝试访问一个null值(非对象)的属性,咱们一步步来排查和修复:

错误原因分析

从你的代码来看,有两个关键点可能导致这个问题:

  1. $currentstatus 可能为 null:当执行 AppraisalGoal::select('is_approved')->where(...)->first() 时,如果没有匹配的记录,first() 会返回 null。如果视图中直接访问 $currentstatus->is_approved,就会触发这个错误。
  2. $identities 也可能为 null:如果 appraisal_identity 表中没有符合 company_id = $userCompany 且 is_current = 1 的记录,$identities 会是 null,后续访问 $identities->id 也会报错(只是当前错误先出在 is_approved 上)。

具体修复步骤

1. 控制器中处理查询返回 null 的情况

修改你的 index 方法,提前判断查询结果是否存在,避免访问非对象的属性:

public function index() {
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    
    // 获取当前身份,处理无匹配记录的情况
    $identities = DB::table('appraisal_identity')
        ->select('id')
        ->where('company_id', $userCompany)
        ->where('is_current', 1)
        ->first();

    // 如果没有找到当前身份,初始化空数据返回
    if (!$identities) {
        $goals = collect();
        $incompleteCount = 0;
        $currentstatus = null;
        return view('appraisal.appraisal_goals.index', compact('goals', 'incompleteCount', 'currentstatus'));
    }

    // 获取当前状态,允许返回null
    $currentstatus = AppraisalGoal::select('is_approved')
        ->where('employee_id', $userEmployee)
        ->where('appraisal_identity_id', $identities->id)
        ->first();

    // 获取目标列表
    $goals = AppraisalGoal::where('employee_id', $userEmployee)
        ->where('appraisal_identity_id', $identities->id)
        ->get();

    // 优化未完成计数:直接用数据库查询代替集合过滤,性能更好
    $incompleteCount = AppraisalGoal::where('employee_id', $userEmployee)
        ->where('appraisal_identity_id', $identities->id)
        ->whereIn('is_approved', [0, 2])
        ->count();

    return view('appraisal.appraisal_goals.index', compact('goals', 'incompleteCount', 'currentstatus'));
}

2. 视图中添加对象存在性判断

假设你的视图中存在访问 $currentstatus->is_approved 的逻辑(比如你可能没贴全代码),必须先判断 $currentstatus 是否为对象:

{{-- 示例:如果有基于currentstatus的判断逻辑 --}}
@if($currentstatus && in_array($currentstatus->is_approved, [0, 2]))
    {{-- 你的相关HTML内容 --}}
@endif

你当前的Edit按钮逻辑是安全的,因为 $goals 是模型集合的实例,每个 $goal 都是 AppraisalGoal 对象(即使集合为空,循环也不会执行)。

3. 可选优化:使用Eloquent关联简化代码

如果你的模型定义了关联关系,代码可以更简洁且符合Laravel最佳实践:

在 User 模型中添加关联:

// User.php
public function currentCompanyIdentity() {
    return $this->hasOne(AppraisalIdentity::class, 'company_id')->where('is_current', 1);
}

public function appraisalGoals() {
    return $this->hasMany(AppraisalGoal::class, 'employee_id');
}

然后控制器代码可以简化为:

public function index() {
    $user = Auth::user();
    $identities = $user->currentCompanyIdentity;

    if (!$identities) {
        $goals = collect();
        $incompleteCount = 0;
        $currentstatus = null;
        return view('appraisal.appraisal_goals.index', compact('goals', 'incompleteCount', 'currentstatus'));
    }

    $currentstatus = $user->appraisalGoals()->where('appraisal_identity_id', $identities->id)->select('is_approved')->first();
    $goals = $user->appraisalGoals()->where('appraisal_identity_id', $identities->id)->get();
    $incompleteCount = $user->appraisalGoals()->where('appraisal_identity_id', $identities->id)->whereIn('is_approved', [0, 2])->count();

    return view('appraisal.appraisal_goals.index', compact('goals', 'incompleteCount', 'currentstatus'));
}

这样代码可读性更高,也减少了手动拼接查询条件的出错概率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 23:12:28