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

Laravel调用destroy函数删除数据后路由跳转失败该如何解决?

修复方案

你当前的问题出在destroy方法仅返回了路由URL字符串,没有返回重定向响应,所以无法触发页面跳转,修改代码如下即可:

public function destroy($locale, $id)
{
    Component::where('id', $id)->delete();
    // 直接使用路由传入的locale参数,返回重定向响应
    return to_route('components.index', compact('locale'));
}

补充说明

  • route()方法仅生成URL字符串,而to_route()方法会直接返回302重定向响应,触发浏览器跳转
  • 如果使用的是8.x以下版本的Laravel,没有to_route辅助函数,可以替换为下面的写法:
return redirect()->route('components.index', ['locale' => $locale]);
  • 原有代码中重复获取$locale的逻辑可以省略,路由传入的$locale参数已经可以直接使用,和资源路由的前缀参数完全匹配,生成的路径就是你需要的{locale}/components格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 13:45:07