【问题标题】:export excel with a query using maatwebsite with laravel [closed]使用带有 laravel 的 maatwebsite 查询导出 excel [关闭]
【发布时间】:2020-06-20 17:40:42
【问题描述】:

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

【问题讨论】:

  • 哪种查询?...发布您迄今为止尝试过的内容(或伪代码以了解您想要的内容)
  • @Berto99 抱歉,我在上面编辑我的问题...你能帮帮我吗?
  • 将excel导出到哪里?
  • @lagbox 完美地导出 excel 但不包括查询 = return Excel::download(new KodePosExport, 'KodePos.xlsx');

标签: php laravel maatwebsite-excel


【解决方案1】:
$download = Export::download(...);

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

return $download;

【讨论】:

  • 导出文件后如何更改页面?我想使用这样的东西 return redirect('/')->with('success', 'All good!');
  • 你不... Export::download 做出下载响应(这是此请求的完整响应),然后您不能再发送另一个响应
【解决方案2】:

我可以在这里提出两种解决方案;

有时,在 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');

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 2020-07-09
    • 2019-05-20
    • 2021-12-10
    • 2016-11-23
    • 1970-01-01
    • 2019-05-15
    • 2017-03-07
    • 1970-01-01
    相关资源
    最近更新 更多