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

Laravel页面重定向时如何显示多个Toastr通知

解决Laravel重定向后显示多条Toastr通知的方案

后端代码修改

将多条通知打包为数组,通过with()方法存入Session:

public function store(Request $request)
{
    // 执行表单提交、邮件发送等业务逻辑

    // 定义多条通知
    $notifications = [
        ['type' => 'success', 'message' => '表单提交成功'],
        ['type' => 'success', 'message' => '邮件发送成功']
    ];

    return redirect(route('my.page.index'))->with('toastr_notifications', $notifications);
}

前端代码修改

修改页面中的Toastr脚本,遍历Session中的通知数组,逐个调用对应类型的Toastr方法:

<script>
    @if(Session::has('toastr_notifications'))
        @foreach(Session::get('toastr_notifications') as $notification)
            // 使用e()函数转义消息内容,避免JS语法错误
            toastr.{{ $notification['type'] }}("{{ e($notification['message']) }}");
        @endforeach
    @endif
</script>

原方案失效原因

你之前尝试的多维数组传递方式,后端虽然传了messages数组,但前端代码仍只检查单条的message字段,没有遍历数组的逻辑,因此无法触发Toastr显示新消息。上述方案无需修改Toastr源码,也不需要AJAX请求,完全适配你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 12:52:04