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

Laravel Nova中Model的static::saved事件无法加载关联关系

Laravel Nova 创建Post时模型事件关联关系失效问题

主应用使用Laravel Nova,遇到模型事件的特定场景问题:
创建新Post模型时,通过模型事件将「最新发布日期」同步到对应的Author模型,核心代码如下:

static::saved(function (Post $post) {
    $post->author->latest_post_date = data_get($post->author->latestPost(), 'latest_post_date');
    $post->author->save();
});

该逻辑在常规应用流程、生产环境均正常,且有测试覆盖,支持编辑已有文章调整日期时同步更新。

问题仅在Laravel Nova中触发:通过Nova创建新Post时,模型事件接收的Post实例无法获取关联关系,调用$post->author返回null。

已尝试以下操作,均无效:

  • $post->refresh();
  • $post->load('author');
  • $post->author()->first();

进一步排查发现,$post->author_id字段已正确存储对应值,但执行Author::find($post->author_id)仍返回null,且该Author实例预先存在,并非本次流程创建。

已查阅Laravel Nova的Resource类及相关Trait,未发现关联关系被移除或禁用的逻辑;重载Resource的afterCreate方法后,问题依然存在。

仅在通过Laravel Nova直接创建Post时出现此问题,其他场景创建Post均正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 20:58:14