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

您好,我真的是使用 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!');

}


holdtom
浏览 167回答 2
2回答

MMMHUHU

$download = Export::download(...);KodePos::query()->truncate();return $download;

子衿沉夜

我可以在这里建议两种解决方案;可终止中间件有时,中间件可能需要在 HTTP 响应发送到浏览器后执行一些工作。如果您在中间件上定义了终止方法并且您的 Web 服务器使用 FastCGI,则在响应发送到浏览器后将自动调用终止方法。创建异步作业并在下载过程之前延迟触发它(您的队列驱动程序不应该是sync,redis是一个很好的候选者)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');
打开App,查看更多内容
随时随地看视频慕课网APP