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

Laravel集合查询无结果:ItemNotFoundException报错排查

Laravel集合查询抛出ItemNotFoundException问题排查

我在Laravel命令行脚本中,将JSON文件转换为集合后执行查询,已打印变量确认数据无误,但仍抛出Illuminate\Support\ItemNotFoundException错误。

我的Laravel代码

$json = File::get(base_path().'/json/Notifications.json');

$notifications = json_decode($json, true);

$notifications = collect($notifications);

echo count($notifications);
echo $this->time;

$dayOfTheWeek = Carbon::now()->dayOfWeek;
       
$todayNotification = $notifications->where('day','=',$dayOfTheWeek)->firstOrFail();

Notifications.json文件内容

[    
    { "day":1, "time":1,"title":"woo", "body":"notifBody1", "image" : "default"},    
    { "day":1, "time":2,"title":"woo", "body":"notifBody2", "image" : "default"},    
    { "day":1, "time":3,"title":"woo", "body":"notifBody3", "image" : "default"},    
    { "day":2, "time":1,"title":"woo", "body":"notifBody4", "image" : "default"},    
    { "day":2, "time":2,"title":"woo", "body":"notifBody5", "image" : "default"},    
    { "day":2, "time":3,"title":"woo", "body":"notifBody6", "image" : "default"},    
    { "day":3, "time":1,"title":"woo", "body":"notifBody7", "image" : "default"},    
    { "day":3, "time":2,"title":"woo", "body":"notifBody8", "image" : "default"},    
    { "day":3, "time":3,"title":"woo", "body":"notifBody9", "image" : "default"},    
    { "day":4, "time":1,"title":"woo", "body":"notifBody10", "image" : "default"},    
    { "day":4, "time":2,"title":"woo", "body":"notifBody11", "image" : "default"},    
    { "day":4, "time":3,"title":"woo", "body":"notifBody12", "image" : "default"},    
    { "day":5, "time":1,"title":"woo", "body":"notifBody13", "image" : "default"},    
    { "day":5, "time":2,"title":"woo", "body":"notifBody14", "image" : "default"},    
    { "day":5, "time":3,"title":"woo", "body":"notifBody15", "image" : "default"},    
    { "day":6, "time":1,"title":"woo", "body":"notifBody16", "image" : "default"},    
    { "day":6, "time":2,"title":"woo", "body":"notifBody17", "image" : "default"},    
    { "day":6, "time":3,"title":"woo", "body":"notifBody17", "image" : "default"},    
    { "day":7, "time":1,"title":"woo", "body":"notifBody18", "image" : "default"},    
    { "day":7, "time":2,"title":"woo", "body":"notifBody19", "image" : "default"},    
    { "day":7, "time":3,"title":"woo", "body":"notifBody20", "image" : "default"}
]

问题原因及解决方法

问题出在日期匹配的数值范围不一致:

  • Carbon的dayOfWeek返回值是0(周日)到6(周六)
  • 你的JSON文件中day字段是1到7(推测1=周一,7=周日)

这导致查询时找不到对应数据,触发firstOrFail()抛出异常。

解决方法二选一:

  1. 调整Carbon的日期转换逻辑,把dayOfWeek转为JSON对应的格式:
    // 如果JSON里1=周一、7=周日,将Carbon的0(周日)转为7,其余保持1-6
    $dayOfTheWeek = Carbon::now()->dayOfWeek === 0 ? 7 : Carbon::now()->dayOfWeek;
    
  2. 修改JSON文件,将day字段改为0-6(0=周日,6=周六),和Carbon返回值对齐。

另外,建议在查询前打印$dayOfTheWeek的值,确认匹配逻辑是否正确:

dd($dayOfTheWeek); // 查看当前输出的数值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:27:52