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

Laravel 8模型Scope方法仅生产环境返回错误值求助

Laravel 8生产环境Scope统计异常问题

我在使用Laravel 8开发时遇到一个仅在生产服务器出现的异常,本地环境完全正常:

模型Scope方法定义

Student模型中定义了以下Scope方法:

public function scopeWhereAlreadySubmitData($query)
{
    return $query->where('status_data_id', '!=', 1);
}

public function scopeWhereAlreadySubmitDocument($query)
{
    return $query->where('status_document_id', '!=', 1);
}

控制器调用逻辑

在AJAX控制器的getAggregates方法中调用上述Scope并执行统计:

public function getAggregates()
{
    $student = Student::with(['something', 'somethingElse']);
    $count_student_already_submit_data = $student->whereAlreadySubmitData()->count();
    $count_student_already_submit_document = $student->whereAlreadySubmitDocument()->count();

    return compact('count_student_already_submit_data', 'count_student_already_submit_document');
}

异常情况说明

  • 本地环境中两个统计值均返回正确结果;
  • 生产环境中count_student_already_submit_data数值正确,但count_student_already_submit_document始终返回0,然而数据库中存在大量status_document_id != 1的记录;
  • 在生产环境tinker中单独调用scopeWhereAlreadySubmitDocument能返回正确数值;
  • Student模型实际有4个类似的Scope方法,仅这一个出现异常;另有一个控制器使用所有Scope方法,均能返回正确结果。

请问可能的原因是什么?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 23:15:16