为了账号安全,请及时绑定邮箱和手机立即绑定

使用 maatwebsite 和 laravel 进行查询导出 excel

使用 maatwebsite 和 laravel 进行查询导出 excel

PHP
holdtom 2023-06-24 15:52:57
您好,我真的是使用 maatwebsite 的新手,已经阅读了文档,但找不到它,有人可以给我一个关于使用 maatwebsite 导出 excel 后执行查询的想法吗?这是我的代码:public function export(){    Excel::download(new KodePosExport, 'KodePos.xlsx');    KodePos::query()->truncate();    return redirect('/')->with('success', 'All good!');}它可以重定向到我想要的页面并截断数据,但不能导出 Excel,我该怎么做?谢谢如果使用此功能导出 Excel 工作正常,但不包含查询public function export(){    return Excel::download(new KodePosExport, 'KodePos.xlsx');    //KodePos::query()->truncate();    // return redirect('/')->with('success', 'All good!');}
查看完整描述

2 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

$download = Export::download(...);


KodePos::query()->truncate();


return $download;


查看完整回答
反对 回复 2023-06-24
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

我可以在这里建议两种解决方案;

  • 可终止中间件

有时,中间件可能需要在 HTTP 响应发送到浏览器后执行一些工作。如果您在中间件上定义了终止方法并且您的 Web 服务器使用 FastCGI,则在响应发送到浏览器后将自动调用终止方法。

  • 创建异步作业并在下载过程之前延迟触发它(您的队列驱动程序不应该是syncredis是一个很好的候选者)

class KudeposTruncater extends Job implements ShouldQueue

{

    use InteractsWithQueue;

    

    public function handle()

    {

        KodePos::query()->truncate();

    }

}

\Queue::later(15, new KudeposTruncater());


return Excel::download(new KodePosExport, 'KodePos.xlsx');


查看完整回答
反对 回复 2023-06-24
  • 2 回答
  • 0 关注
  • 151 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信